我正在开发一个程序,提示用户输入一些浮点数。在用户输入他们想要的数字量之后,他们输入结束用户输入的数字0并输出输入的数量,这些数字的平均值以及输入的所有数字的总和。到目前为止我已经完成了所有这些工作,但我想让程序输出用户输入的最高和最低浮点数。我该怎么做呢?我尝试使用max()和min()但不会使用我能说的浮点数。
我的代码目前在哪里:
print("Input some floats. Input 0 to exit.")
count = 0
sum1 = 0.0
number = 1.0
while number != 0 :
number = float(input(""))
sum1 = sum1 + number
count += 1
if count == 0 :
print("Input some numbers")
else:
print("Average: ", sum1 / (count-1))
print("Sum: ", sum1)
print("Numbers entered: ", count)
答案 0 :(得分:2)
您只需要列表来存储输入。见下面的代码
print("Input some floats. Input 0 to exit.")
count = 0
sum1 = 0.0
number = 1.0
list_n =[]
while number != 0:
number = float(input(""))
sum1 = sum1 + number
if number != 0 :
list_n.append(number)
count += 1
if count == 0:
print("Input some numbers")
else:
print("Average:", sum1 / (count-1))
print("Sum:", sum1)
print("Numbers entered:", count)
print("Max: ", max(list_n))
print("Min: ", min(list_n))
输出:
Input some floats. Input 0 to exit.
4
5
8
1
0
Average: 4.5
Sum: 18.0
Numbers entered: 5
Max: 8.0
Min: 1.0