-4

What is the problem with this syntax: (this is Python 2)

num=""
word=[]

while ((num = raw_input("Enter a number: "))!='done'):

    if num!='done':
        word.append(num)
print 'Maximum: ',float(max(word))
print 'Minimum: ',float(min(word))

Why the user input can't be in the while loop? Or how can I put this in the while condition?

I don't need the solution to resolve this problem I am only ask why Python does not allow this?

here is my code to solve this problem: (finding the max and min form list until user enter "done")

num=''
word=[]

while (num!='done'):
    num = raw_input("Enter a number: ")
    if num!='done':
        word.append(num)
print 'Maximum: ', float(max(word))
print 'Minimum: ', float(min(word))
  • 1
    Why do you expect that to work? What in the language specification made you think this is valid? – too honest for this site Mar 20 '18 at 15:09
  • The answer is in the duplicate question. Also you will have a problem with `min()` and `max()` not returning the expected values since `word` is a list of strings and string ordering is not numerical but lexicographic. You want to convert your user inputs to floats (since you seem to expect floats) before storing them in `word`. – bruno desthuilliers Mar 20 '18 at 15:16

1 Answers1

-1

You could try something like this:

num=""
word=[]

while True:
    num = raw_input("Enter a number: ")
    if num!='done':
        word.append(float(num))
    else:
        break
print 'Maximum: ',max(word)
print 'Minimum: ',min(word)

Essentially, you need to assign a value to num outside of your condition and inside of the actual loop, and then tell the while loop to stop when your variable is 'done'. Also, I think you should cast the number as a float when it's being added to your list so that max and min can be called naturally.

D. Rad
  • 125
  • 1
  • 10