Search code examples
python-2.7floating-pointraw-input

Python's min() function does not work with raw_input converted to float


[Running on Xubuntu 12.04 LTS, Python 2.7]

Hey. I am struggling a bit with this assignment. It is from book about learning Python but I am probably missing something here. I do not require complete answer but a hint what should I do would be much better than copy/paste it.

My goal now is to create code that counts Smallest number so far from all the user inputs. I know that it has to do something with an impossibility of using min() and "for loops" for float numbers/single numbers as it is necessary to have some list but I do not have any idea what to do now..

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"

Thank you very much for tips and hints on what to do next.


Solution

  • The easiest way may be:

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

    and then the total is sum( Smallest ), the smallest is min( Smallest ) and the number is len( Smallest ). You are storing all the intermediate values, which is not really necessary, but I think here is the simplest.