Domanda

I am trying to build a list with raw inputs.

while True:
    inp = raw_input("Enter a number: ")

    #edge cases
    if inp == "done" : break
    if len(inp) < 1 : break

    #building list 
    try:
        num_list = []
        num = int(inp)
        num_list.append(num)
    except:
        print "Please enter a number."
        continue

    #max and min functions
    high = max(num_list)
    low = min(num_list)

#print results 
print "The highest number: ", high
print "The lowest number: ", low

print "Done!"

At the moment it seems to only save one of the inputs at a time and therefore the the last raw input is printed as both the max and min.

Any ideas? I am new to Python and could use some direction. So far I have been unable to find the answer in StackOverflow or in the Python documentation. Thanks in advance!

È stato utile?

Soluzione

That's because you keep erasing the list with each iteration. Put num_list = [] outside the while loop:

num_list = []
while True:
    ...

You should also put these two lines:

high = max(num_list)
low = min(num_list)

outside the loop. There is no reason to keep executing them over and over again.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top