-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))
2
  • 1
    Why do you expect that to work? What in the language specification made you think this is valid? Commented Mar 20, 2018 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. Commented Mar 20, 2018 at 15:16

1 Answer 1

-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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.