1

I'm taking my first-ever Python class and, like most Python classes, the last assignment is to create a guessing game from 1-100 that tracks the number of VALID tries. The element that I just cannot get (or find here on stackoverflow) is how to reject invalid user input. The user input must be whole, positive digits between 1 and 100. I can get the system to reject everything except 0 and <+ 101.

The only things I can think to do end up telling me that you can't have operators comparing strings and integers. I keep wanting to use something like guess > 0 and/or guess < 101. I've also tried to create some sort of function, but can't get it to work right.

#  Generate random number
import random
x = random.randint(1,100)

#  Prompt user for input
print("I'm thinking of a number from 1 to 100")
counter = 0

while True:
    guess = input("Try to guess my number: ")

    #  Check if input is a positive integer and is not 0 or >=101
    #  this line doesn't actually stop it from being a valid guess and 
    #  counting against the number of tries.
    if guess == "0":  
        print(guess, "is not a valid guess")
    if guess.isdigit() == False: 
        print(guess, "is not a valid guess")
    else: 
        counter += 1
        guess = int(guess)

        #  Begin playing
        if guess > x:
            print(guess, "is too high.")
        elif guess < x:
            print(guess, "is too low.")
        else:
            print(guess, "is correct! You guessed my number in", counter, "tries!")
  • 2
    Look up Try and how to handle it if the try fails. The user will pass a word, you Try to convert it to an int and if that fails, you give an output that the input is invalid. – dfundako May 25 '18 at 15:26
  • 3
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – chevybow May 25 '18 at 15:26
  • 2
    `int(guess)` should be the first thing to do. If that fails, it's not an integer. Then you only need to check if its less than 1 or greater than 100, which is easy since it will already have been converted to an integer. – mkrieger1 May 25 '18 at 15:26
  • Rather than separately accounting for all cases where the guess is invalid, try creating a single test for whether the guess is valid. – Silenced Temporarily May 25 '18 at 15:29

3 Answers3

-1
import random
x = random.randint(1,100)

#  Prompt user for input
print("I'm thinking of a number from 1 to 100")
counter = 0

while True:
    guess = input("Try to guess my number: ")
    try:
        guess = int(guess)
        if(100 > guess > 0):
            counter += 1
            guess = int(guess)
            #  Begin playing
            if guess > x:
                print(guess, "is too high.")
            elif guess < x:
                print(guess, "is too low.")
            else:
                print(guess, "is correct! You guessed my number in", counter, "tries!")
                break
        else:
            print("Number not in range between 0 to 100")
    except:
        print("Invalid input")
-1
#  Generate random number
import random
x = random.randint(1,100)

#  Prompt user for input
print("I'm thinking of a number from 1 to 100")
counter = 1   


while True:
    try:
        guess = int(input("Try to guess my number: "))
        if guess > 0 and guess < 101:
            print("That's not an option!")
        #  Begin playing
        elif guess == x:
            print(guess, "is correct! You guessed my number in", counter, "tries!")
            break
        elif guess > x:
            print(guess, "is too high.")
        elif guess < x:
            print(guess, "is too low.")
        else:
            counter += 1        
    except:
        print("That's not a valid option!")
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
-1

My instructor helped me out. (I posted to keep from needing that from the guy who's giving me the grade.) Here is what we came up with. I'm posting it to help out any future Python learner that may have this particular rejecting user input problem.

Thank you guys for posting SO FAST! Even though I needed the instructor's help, I would've looked even more incompetent without your insights. Now I can actually enjoy my holiday weekend. Have a great Memorial Day weekend!!!

import random
x = random.randint(1,100)

print("I'm thinking of a number from 1 to 100.")
counter = 0

while True:
    try:        
        guess = input("Try to guess my number: ")
        guess = int(guess) 
        if guess < 1 or guess > 100:
            raise ValueError()
        counter += 1
        if guess > x:
            print(guess, "is too high.")
        elif guess < x:
            print(guess, "is too low.")
        else:
            print(guess, "is correct! You guessed my number in", counter, "tries!")
            break
    except ValueError:
        print(guess, "is not a valid guess")