-2
import random
print("Welcome to RNG Guesser!\n")
gld = random.randrange(1,10)
counter = 0
ccounter = 0

while True:
    print("Number of tries: {}".format(counter))
    print("Number of correct guesses: {}".format(ccounter))

num = input("Enter a number: ")

if num is "exit":
    print("Number of tries: {}".format(counter))
    print("Number of correct guesses: {}".format(ccounter))
    break
else:

    if int(num) is gld:
        print("Congratulations, your guessed number {} was right!".format(num))
        counter += 1
        ccounter += 1

    elif int(num) < gld:
        print("Pick a higher number!")
        counter += 1

    else:
        print("Pick a lower number!")
        counter += 1

Why am I getting the "invalid literal for int" when I type in exit? I tried converting the input variable to int, I tried with an else statement, I tried making 2 variables, one for string one for int, and none of them worked.

FallenRune
  • 11
  • 5

3 Answers3

0

Assuming that the incorrect indentation in the question is just a copy-paste mistake... try this:

x = input('enter x > ')
print('x == "exit": {}'.format(x == "exit"))
print('x is "exit": {}'.format(x is "exit"))

Here's what happens:

enter x > exit
x == "exit": True
x is "exit": False

Or maybe:

x is "exit": True

The is operator compares object identity but you are trying to compare the contents of two strings.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    Assuming the indentation is correct, his code would be stuck in an infinite loop. – Vincent Savard Feb 12 '16 at 20:45
  • I mean "assuming your indentation is correct, unlike the indentation in the question." However, it wouldn't be an infinite loop if it were indented like the question--it would be a `SyntaxError`. – Dietrich Epp Feb 12 '16 at 20:52
0

I believe the issue is from the line: if num is "exit"

Is evaluating to False and further down the script when Python tries to convert the literal string exit to an int, it will fail.

Try replacing is with ==

The problem is that is compares two objects to see if they are the same, whereas what want to is to see if the tow objects' values are the same. Check this stack overflow thread for more info.

Community
  • 1
  • 1
Evan Bare
  • 16
  • 1
0

Note that you cannot give a string with non-numeric characters to int().

Now num is supposed to be a str, and it could be anything from user input. Also note that when you want to evaluate two values, use == instead of is. is is supposed to be used as judging if two things are the same object.

If you want to use if-else, try this:

if num == "exit":
    print("Number of tries: {}".format(counter))
    print("Number of correct guesses: {}".format(ccounter))
    break
elif not num or not all(char.isdigit() for char in num):
    print("You are not giving a number.")
else:
    if int(num) == gld:
        print("Congratulations, your guessed number {} was right!".format(num))
        counter += 1
        ccounter += 1

    elif int(num) < gld:
        print("Pick a higher number!")
        counter += 1

    else:
        print("Pick a lower number!")
        counter += 1

Here, all(char.isdigit() for char in num) is checking for every character in num to see if they are all numbers. We should be aware that anything could appear in user's input. Only numbers can be converted to int.

We have another solution which is more clear and simple. You may need to read some documents on try...except... in Python.

try:
    if int(num) ...
except ValueError:
    # num is not able to be converted to int
    print("You are not giving a number.")
kagami
  • 592
  • 4
  • 15
  • The validation `all(char.isdigit() for char is num)` incorrectly accepts empty strings. Easy way to fix this is to just use `try: ... int(num) ... except ValueError:` – Dietrich Epp Feb 12 '16 at 20:54
  • @DietrichEpp Yes you're right, I wanted to provide the solution using `try...except` but finally sticked to the structure he used. I will edit the answer. – kagami Feb 12 '16 at 20:59