简体   繁体   中英

Finding largest and smallest number where user inputs numbers using a loop

I am creating a program where I am asking the user to input numbers using a loop (not function).
I need to assume the numbers are floating. User will continue to input numbers until they hit the enter key a second time. Then the program will generate the largest number, smallest number, sum of the list and average number.

I've figured everything out but when I type in a number 10 or larger, the program doesn't identity it as the largest. In fact, I noticed that 10 is considered the smallest number.

Also I'm not able to convert the numbers in floating numbers.

myList = []

done = False
while not done:

    enterNumber = raw_input("Enter a number (press Enter key a second time to stop): ")
    nNumbers = len(myList)

    if enterNumber == "":
        done = True
        break
    else:
        myList.append(enterNumber)

print "You've typed in the following numbers: " + str(myList)

smallest = 0 
largest = 0

for nextNumber in myList:                          
    if smallest == 0 or nextNumber < smallest:
        smallest = nextNumber

for nextNumber in myList:        
    if largest == 0 or largest < nextNumber:
        largest = nextNumber

print "Largest number from this list is: " + largest
print "Smallest number from this list is: " + smallest

sum = 0
for nextValue in myList:
    sum = sum + float(nextValue)
print "The sum of all the numbers from this list is: " + str(sum)

average = float(sum/nNumbers)
print "The average of all the numbers from this list is: " + str(average)

This tells me that you are probably not comparing what you think :)

Your code is mostly correct, except for the fact that you never told python what the "type" of the user input was. I am guessing python assumed the user entered a bunch of strings, and in the string world - "10" < "9"

So in your code, force the type of "enterNumber" to be a float before adding it to your list of floats like so:

while not done:
    enterNumber = raw_input("Enter a number (press Enter key a second time to stop): ")
    nNumbers = len(myList)

    if enterNumber == "":
        done = True
        break
    else:
        myList.append(float(enterNumber))

you need to convert it before you add it to your list

enterNumber = raw_input("Enter a number (press Enter key a second time to stop): ")
nNumbers = len(myList)

if enterNumber == "":
    done = True
    break
else:
    myList.append(float(enterNumber)) # convert it here...
    #you may want to check for valid input before calling float on it ...

but my favorite way to get a list of numbers is

my_numbers = map(float,
                 iter(lambda:raw_input("Enter a number or leave blank to quit").strip(),""))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM