我似乎无法弄清楚为什么我的程序会运行以及修复什么,以便乘法发生的部分会给我一个用户输入的所有数字的乘积。
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 :(得分: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))