1

I'm creating an application for my programming class and I'm unable to get it to run properly. Essentially, the application should take 8 numbers from the user and store them in an array and then add those numbers. However, if the user does not provide a number, or press Q, the program should stop.

userNumberList = []
counter = 0
while counter < 8:

    try:
        userNumber = int(input("Welcome! Please provide numbers or press q to quit. "))
    except ValueError:
        print("Not a number. Closing application.")
        break
    else:
        if userNumber == 'q':
            break
        else:
            userNumberList.append(int(userNumber))
            counter += 1

print(sum(userNumberList))

This is the error I get when running typing a String instead of a number in the prompt:

userNumber = int(input("Welcome! Please provide numbers or press q to quit. "))
ValueError: invalid literal for int() with base 10:
  • I feel like we need a canonical question specifically about trying to convert the letter `q` to an int... – Aran-Fey May 24 '18 at 22:15
  • Anyway, the code you posted doesn't throw that exception. Your indentation is wrong (the code throws a SyntaxError), and if I fix that then the ValueError is caught correctly. Please post a [mcve]. – Aran-Fey May 24 '18 at 22:17
  • Please can you fix the indentation? I can't see why you'd get that error. Also, we can use a full traceback here. – roganjosh May 24 '18 at 22:17
  • Code has been indented. Sorry about that. –  May 24 '18 at 22:21
  • 1
    Even with your indentation fixed I don't get that. –  May 24 '18 at 22:22
  • `int(input( ` cast the int later in the process. – Lex May 25 '18 at 00:53
  • And use `raw_input` over `input`. Tested it out, works a treat. `userNumber = raw_input("Welcome! Please provide numbers or press q to quit. ")` – Lex May 25 '18 at 00:57

2 Answers2

2

Try this: Do not convert to int before checking to q.

userNumberList = []
counter = 0
while counter < 8:
    userNumber = input("Welcome! Please provide numbers or press q to quit. ")
    if userNumber == 'q':
        print("Entered command to quit!! closing the application")
        break
    else:
        try:
            userNumberList.append(int(userNumber))
            counter += 1
        except ValueError:
            print("Not a number. Closing application.")
            break
print(sum(userNumberList))
  • Thanks for the help! When I run it and type Q to quit or provide a String instead of an int, I get the error: `ValueError: invalid literal for int() with base 10: ` –  May 24 '18 at 22:46
0

Don't cast the input to an int straight away. You're handling that when appending anyway.

And apprently input evaluates input as python code. Use raw_input instead. This answer may be helpful Python 2.7 getting user input and manipulating as string without quotations

userNumber = raw_input("Welcome! Please provide numbers or press q to quit. ")

Changing this line fixes the program.

Lex
  • 4,749
  • 3
  • 45
  • 66
  • This is not an answer. The question is tagged as Python3 and seemingly the code is Python3. `raw_input` is not defined in Python3. – Mr. T May 25 '18 at 06:11
  • @Mr.T its also tagged with `python`, the code 'works' with that line amended. Your comment might be friendly/helpful mentioning that `raw_input` was renamed to `input` in python3 – Lex May 25 '18 at 08:03