2017-09-14 3 views
-5

なぜ私のプログラムが実行され、何を修正すれば乗算が起こるのか分かりませんユーザーが入力するすべての数字の積。Pythonはエラーなしでプログラムを評価しますが、合計と平均は同じですが、製品は常に同じです

flag = False 
rNum = 0 
num = '' 
sum = 0 
product = 1 
count = 0 
temp = input("Please enter your numbers separated by a space, Press the 
Enter Key when finished >> ") 

for i in range(len(temp)): 
if temp[i] != ' ': 
    if temp[i] == '-': 
     flag = True 
    else: 
     num += temp[i] 
if temp[i] != ' ': 
    def multiply(rNum): 
    total = 1 
    for i in range(0, len(n)): 
     total *= [i] 
     product *= rNum 
if i == len(temp) - 1 or temp[i] == ' ': 
    rNum = int(num) 
    if flag == True: 
     sum -= rNum 
    else: 
     sum += rNum 
    flag = False 
    count += 1 
    num = '' 

    print ("sum: %d product: %d average: %d" %(sum, product, sum/count)) 
+0

あなたは 'python -m pdb <あなたのファイル名here.py>'または 'import pdb;を挿入しようとしましたか?何が起こっているのかを知るために[python debugger、pdb](https://docs.python.org/3/library/pdb.html)を使うためのpdb.set_trace()あるいは、 'print'ステートメントを挿入して値を表示しようとしましたか?あなたが試したことを私たちに伝えていないので、あなたは落選しています。 – Darthfett

+1

「正しく評価されていません」は、有用な問題の説明ではありません。あなたは何をしようとしていたのですか?エラー・メッセージが表示された場合は、コード・フォーマットされたスタック・トレースを含めて通知してください。出力が間違っている場合は、コードフォーマットされた正確な出力だけでなく、投稿してください。 – user2357112

+2

@ChazingtonPerkあなたは 'multiply'関数を定義しますが、コード内のどこでも' multiply() 'を使用したり、' multiply() 'を呼び出すことはないので、' product'を印刷するときに ' – davedwards

答えて

0

私は、タスクを複雑にしているので、チュートリアルをもっと詳しく読む必要があると思います。このコードは、タスクがどのように達成されるかを知ることができます。

text = input("Please enter your numbers separated by a space,\nPress the Enter Key when finished >> ") 

# if you want to split a text string at whitespace use split() 

text_numbers = text.split() 

# then if you want to convert the text to a number 

numbers = [float(i) for i in text_numbers]  

# to add the numbers together use sum() 

total = sum(numbers) 

# to find the product and keeping the method obvious 
product = 1 
for i in numbers : 
    if i != 0 : 
     product *= i 

# the number of numbers can be found from the length of the list, len() 

average = total/len(numbers) 

print ("total: %.3f product: %.3f average: %.3f" % (total, product, average))
関連する問題