1

I'm trying to make a little guessing game in python by using the random import.

Problem is, I want the user to be able to enter "exit" to end, but I also need to compare their data to the random number. My thinking now is, how can I ask the user for an int, but also check if they entered a string called exit?

The code is supposed to let them keep playing if they guess it right.

so far i have this.

import random

num = random.randint(1, 100)

guess = input("Enter your guess: ")

while guess != "exit":
    if guess.isdigit() == False:
        guess = input("Please enter valid number: ")
    elif guess > num:
        print("Lower!")
    elif guess < num:
        print("Higher!")
    elif guess == num:
        print("YOU GOT IT!!!!!")
        print("Setting new number.")
        num = random.randint(1, 100)
        guess = input("Enter number now: ")




print("Terminating game now.")
2
  • What version of Python? Commented Apr 25, 2018 at 3:33
  • turns out I had two versions of python on my computer, and was fumbling around with errors that I shouldn't have been having. Commented May 5, 2018 at 1:39

1 Answer 1

1

The problem with your code is that you need to call int(guess), and then use the result of that, if you want to compare the value as an int.

The smallest change to your code would be either this:

# everything before this if stays the same
if guess.isdigit() == False:
    guess = input("Please enter valid number: ")
    continue
guess = int(guess)
if guess > num:
    print("Lower!")
# the rest of the code is the same after here

That continue means to skip the rest of the loop body and go back to the while.


If that's confusing, you could rewrite things like this:

# everything before this if stays the same
if guess.isdigit() == False:
    guess = input("Please enter valid number: ")
else:
    guess = int(guess)
    if guess > num:
        print("Lower!")
    # the rest of the code is the same after here (but still indented)

Or, at the cost of some extra repetition (which makes the code more verbose and less efficient, and gives you more changes to get it wrong):

if guess.isdigit() == False:
    guess = input("Please enter valid number: ")
elif int(guess) > num:
    print("Lower!")
elif int(guess) < num:
    print("Higher!")
elif int(guess) == num:
    print("YOU GOT IT!!!!!")
    # etc.

Finally, you might want to consider a slightly bigger change: The cleanest way to check whether a string can be interpreted as an int is to just try to interpret it as an int. Unlike isdigit, this will do the right thing if, say, they include extra spaces before or after the digits, or if they use underscores to separate group of digits, or if they use Unicode digits from some other language. To do that:

try:
    guess = int(guess)
except ValueError:
    guess = input("Please enter valid number: ")
    continue
if guess > num:
    # etc.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.