Python的min()函数不能与raw_input转换为float一起使用

时间:2014-07-25 22:20:07

标签: python-2.7 floating-point raw-input

[在Xubuntu 12.04 LTS上运行,Python 2.7 ]

嘿。我在这项任务中苦苦挣扎。这是关于学习Python的书,但我可能在这里遗漏了一些东西。我不需要完整的答案,但提示我应该做什么比复制/粘贴要好得多。

我现在的目标是创建迄今为止所有用户输入中最小数量的代码。我知道它必须做一些不可能使用min()和“for循环”浮点数/单个数字,因为有必要有一些列表,但我不知道现在该做什么..

Count = 0
Total = 0
Smallest = None #Or maybe I should use something like Smallest = []?

while True:
  user = raw_input("Enter number, when you are finished enter done or press enter: ")
  if len (user) < 1: break
  if user  == "done":
    print "Done entered, program executed!"
    break
  try:
    fuser = float(user)
    print "Valid input.", "Your input:", fuser
  except:
    print "Invalid input.", "Your input:", user
    continue
  Count = Count + 1
  Total = Total + fuser
  #Smallest = None
  #for i in [Total]:
    #if Smallest is None or itervar < Smallest:
      #Smallest = i
# As you can see I've been simply trying to find some way (code with # obviously doesn't work at all...)

#print "Min: ", Smallest 
print "Count: ",Count
print "Total number: ",Total
try:
  print "Average:", Total/Count
except:
  print "NOT AVAILABLE"

非常感谢您提供有关下一步操作的提示和提示。

1 个答案:

答案 0 :(得分:1)

最简单的方法可能是:

Smallest = []
...
Smallest.append( float( user ) )

然后总数为sum( Smallest ),最小值为min( Smallest ),数字为len( Smallest )。您正在存储所有中间值,这不是必需的,但我认为这是最简单的。