1

I've been banging my head against the wall with this task and I can't seem to figure it out for the life of me.

I want to write a script that would prompt a user to input a number and store that number in a list every time after they input the number. When the user would just press enter and not input anything, the script would then print out the highest number in the list.

This is what I wrote so far:

x = 0
mylist = []
while x != '':
    x = input("enter a number:")
    mylist.append(x)
    if x == '':
        print(max(mylist))

There are two problems with this code that I can see:

  1. The user can input a string (I tried fixing that by saying x = int(input(...), but that only lead to a new error:

    ValueError: invalid literal for int() with base 10: ''
    
  2. print(max(mylist)) only prints out the number with the largest initial integer. For instance, if the user inputs 51 and 112, it would print out 51.

Thank you for taking your time in helping me out.

Blitva
  • 121
  • 1
  • 11

2 Answers2

2

Use a different variable to test the While. Check isnumeric() for the input, and if it is numeric, convert it as you append it to your array:

keepMoving = True
mylist = []
while keepMoving:
    x = input("enter a number:")
    if x.isnumeric():
         mylist.append(int(x))
    else:
         keepMoving = False
print(max(mylist))
CLaFarge
  • 1,277
  • 11
  • 16
  • Thank you. Although I got a warning saying "unresolved reference 'isnumeric' ", I suppose you wanted me to do the following: http://pastie.org/10300751 This seems to work now. Would love to know if there is a neater, shorter version to do the same. – Blitva Jul 19 '15 at 11:02
  • If you're using PyCharm, this is known issue... here's a description: https://www.jetbrains.com/pycharm/help/resolving-references.html and a fix: http://stackoverflow.com/questions/11725519/pycharm-shows-unresolved-references-error-for-valid-code – CLaFarge Jul 19 '15 at 14:53
  • You could also use x.isdigit()... note in above I've edited from isnumeric(x) to x.isnumeric(), as well. – CLaFarge Jul 19 '15 at 15:04
  • That would explain it then. Thanks. – Blitva Jul 20 '15 at 16:26
1

I suggest you use a try statement to check if the input is an integer. The following is an implementation which attempts to convert the user input into an integer, and append that to the list. In the result of a ValueError (a non-int input), it will print the largest integer in the list. Using raw_input instead of input is also a good practice here, to prevent python from trying and failing to convert the input on its own.

x=0
mylist=[]

while x!='':
    x = raw_input("enter a number:")
    try:
        x = int(x)
        mylist.append(x)
    except ValueError:
        if x == '':
            print(max(mylist))

This prevents the user from adding non-integers to the list, and is a better way to handle unexpected user input. As an added bonus, this method also makes it easy to add more conditions in the future.

eleventhend
  • 296
  • 3
  • 17
  • Thank you. However it still doesn't solve the issue of it printing out the number with the largest initial integer instead of the actual value. For instance, I input 51, 22, and 124, and instead of printing out 124 it printed out 51. – Blitva Jul 19 '15 at 10:17
  • http://pastie.org/10300751 This version seems to resolve the issue stated above. Still not quite sure how or why though. – Blitva Jul 19 '15 at 11:03
  • Inside the Try, the "int(x)" will fail if the entry is a non-number, and it will not make as far as the mylist.append(x) line. Execution jumps into the ValueError exception, which simply checks to see if empty and if so prints the greatest value. – CLaFarge Jul 19 '15 at 14:58
  • Sorry - the issue was that the original `try` statement in my answer did not actually set x as an int, it just checked to see if it was possible. The updated code above should now do exactly what you need. – eleventhend Jul 19 '15 at 16:20
  • Thank you both. You've both been a real help and I feel like I learned a lot. – Blitva Jul 20 '15 at 16:24