Sharan Aithal
Sharan Aithal

Reputation: 3

Why does Exit code give a ValueError?

Here's the code:

print('Type something you idiot:')
while True:
    spam = str(input())
    if spam == '1':
        print('Hello')
    elif spam == '2':
        print('Howdy')
    elif int(spam) > 2 and int(spam) > 1:
        print('Greetings!')
    elif str(spam) == 'exit':
        sys.exit()
    else:
        print('Type a positive # bruh')
    print('Type again you dumdum:')

Here's the error:

exit
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 9, in <module>
ValueError: invalid literal for int() with base 10: 'exit'

[Program finished]

Other stuff:

I tried googling that last error line but it seemed unrelated to my problem, something about float and all. I want the program to exit when I type exit but I get that error. All the other things seem to be working (1, 2, 3, -1)

Another thing that isn't working is typing something other than "exit". I get the same error message. Spent a lot of time trying to fix it to no avail. Please help, thank you.

Upvotes: 0

Views: 502

Answers (3)

xco
xco

Reputation: 51

As mentionned L.Grozinger the "exit match" position is important.

You may also add ValueError exception try/catch to fix wrong user input...

import sys

print('Type something you idiot:')
while True:
    spam = str(input())
    print(spam)
    try:
        if spam == '1':
            print('Hello')
        elif spam == '2':
            print('Howdy')
        elif spam == "exit":
            sys.exit()
        elif int(spam) > 2 and int(spam) > 1:
            print('Greetings!')

    except ValueError:
        print('Type again you dumdum:')

Upvotes: 0

difurious
difurious

Reputation: 1542

You can't compare integers to strings. So check if the string is an integer before comparing:

print('Type something you idiot:')
while True:
    spam = str(input())
    if spam == '1':
        print('Hello')
    elif spam == '2':
        print('Howdy')
    elif spam.isdigit() and int(spam) > 1:
        print('Greetings!')
    elif str(spam) == 'exit':
        sys.exit()
    else:
        print('Type a positive # bruh')
    print('Type again you dumdum:')

This will stop it falling over if something other than a number or "exit" is entered.

Upvotes: 1

L.Grozinger
L.Grozinger

Reputation: 2398

When you type 'exit' the condition for the third clause in your if statement is evaluated. That is, int(spam) > 2 and int(spam) > 1. But if spam = 'exit', then spam cannot be converted to an int, hence the error.

Reordering your clauses in your if statement is the easiest solution.

print('Type something you idiot:')
while True:
    spam = str(input())
    if spam == '1':
        print('Hello')
    elif spam == '2':
        print('Howdy')
    elif str(spam) == 'exit':
        sys.exit()
    elif int(spam) > 2 and int(spam) > 1:
        print('Greetings!')
    else:
        print('Type a positive # bruh')
    print('Type again you dumdum:')

Now, int(spam) > 2 and int(spam) > 1 is only evaluated if str(spam) != 'exit' which is fine for the (limited) inputs you expect.

Upvotes: 2

Related Questions