2

Below is my code for guessing a random number. I had to check the input to make sure it was an integer and within the 1-20 range. Everything is working. It outputs the right response if it is not an integer or is out of range but then it continues through the while loop. I thought the try except would send it back before it continued. What have I done incorrectly here? I cannot figure out the problem. Thank you for any help!

import random

tries = 0

name=(raw_input('Hello! What is your name? '))

number = random.randint(1, 20)
print('Hello, ' + name + ', I am thinking of a number between 1 and 20.')


while tries < 6:

    guess = (raw_input('Take a guess.' ))

    try:
        guess = int(guess)
    except ValueError:
        print 'You did not enter a valid number, try again.'    

    tries = tries + 1

    if guess<1 or guess>20:
        print 'Your guess is not between 1 and 20'

    if guess < number:
        print 'Your guess is too low.' 
    if guess > number:
        print 'Your guess is too high.'

    if guess == number:
        break

if guess == number:
    print 'Good job, ',name,'! You guessed my number in ',tries,' guesses!'

if guess != number:
     print 'Sorry, The number I was thinking of was ',number

3 Answers 3

2

All you do is when a ValueError is raised is to have an extra line printed. If you want your loop to start from the beginning, add continue in the except block. If you want to count invalid inputs as tries, move the line where you increment tries to the beginning of your loop.

tries += 1 # increment tries here if invalid inputs should count as a try, too
           # increment tries after the except block if only valid inputs should count as a try
# get input here
try:
    guess = int(guess)
except ValueError:
    # inform user that the input was invalid here
    continue # don't execute the rest of the loop, start with the next loop

You can either put another continue where you check whether the number is too high or too low:

if guess<1 or guess>20:
    print 'Your guess is not between 1 and 20'
    continue

or use an if/elif construct:

if guess<1 or guess>20:
    print 'Your guess is not between 1 and 20'
elif guess < number:
    print 'Your guess is too low.' 
elif guess > number:
    print 'Your guess is too high.'
else: # guess == number
    break

I recommend the if/elif. Having multiple continues in a loop can make it hard to follow.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, that worked for the invalid input. Now my only issue is the range with my if statement. I though putting the counter after that would prevent it from counting it as a try but again it continues through the rest of the while loop. Grrrr. Sorry very new to programming and getting frustrated that I cannot complete such an easy program.
If you want invalid inputs not counted as a try, leave the line where you increment tries where it currently is. Just add the continue to your except block. Does that work for you?
The continue worked for the invalid input but I have to check to make sure the input is also between 1 and 20. I put the count after the if statement for that check and it is still going through the while loop. Do I need an else statement after my range check. This is what I'm getting as output now: Take a guess.dk You did not enter a valid number, try again. Take a guess.59 Your guess is not between 1 and 20 Your guess is too high. Take a guess.
@danie I see the problem, give me a second
Thank you! I got it now. Thank you so much for the help. Read my answer for more info. :-)
1

You told it to print something, but Python doesn't know that you don't want it to do other things during that iteration. Sometimes you might want it to go on only if there was an error. To say "skip this loop", use continue:

try:
    guess = int(guess)
except ValueError:
    print 'You did not enter a valid number, try again.'
    continue

1 Comment

Thank you! I got it now. Thank you so much for the help. Read my answer for more info. :-)
1

You need to continue after both tests and only increment the tries once you have successfully passed the tests.

import random
tries = 0
name=(raw_input('Hello! What is your name? '))
number = random.randint(1, 20)
print('Hello, ' + name + ', I am thinking of a number between 1 and 20.')

while tries < 6:

    guess = (raw_input('Take a guess.' ))

    try:
        guess = int(guess)
    except ValueError:
        print 'You did not enter a valid number, try again.'
        continue

    if guess<1 or guess>20:
        print 'Your guess is not between 1 and 20'
        continue

    tries = tries + 1

    if guess < number:
        print 'Your guess is too low.'
    if guess > number:
        print 'Your guess is too high.'

    if guess == number:
        break

if guess == number:
    print 'Good job, ',name,'! You guessed my number in ',tries,' guesses!'

if guess != number:
     print 'Sorry, The number I was thinking of was ',number

1 Comment

Thank you! I got it now. Thank you so much for the help. Read my answer for more info. :-)

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.