-2

I'm writing a program that is a guessing game where the user has one chance to guess a number between 1 and 20.

There are two problems with the code:

Number one is still the input validation. The firstGuess variable is actually in main().

firstGuess = userguess()    
def userGuess():
    while True:
        try:
            guess = int(input('Enter a number between 1 and 20: '))
            if 1 <= guess >= 20:
                return guess
        except ValueError:
            print (guess, 'is not a valid guess!')
        break

What I'm trying to do is put the input validation in a loop (while True:) until the user gives good input (in this case a positive number between 1 and 20). If the user were to enter 'd', or '-5', the program should continue looping until good input is given. However, this is not the case. By adjusting the code, I have been able to ask for the input again once bad input is entered, but if bad input is given a third time I get "another exception occured while handling this exception."

*Removed other problem, @Henry Woody was correct in that I wasn't using the conditionals correctly.

deHart
  • 155
  • 1
  • 9
  • 1
    Condition not quite right. Use `if 1 <= guess <= 20:` – Austin Oct 28 '18 at 18:34
  • 2
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – mkrieger1 Oct 28 '18 at 18:40
  • Played with code just a *bit* more and got it working, the sense of relief I feel is immense, and wouldn't have been possible without the help I got here, thanks guys! – deHart Oct 28 '18 at 19:01

1 Answers1

0

The issues are with the conditionals.

The first is the line:

if 1 <= guess >= 20:

in userGuess, which is checking for a number greater than or equal to 20, which is not what you want. You can fix this issue by changing the condition to:

if 1 <= guess <= 20:

Next, the conditional:

if guess1 == randomOne or randomTwo or randomThree:

checks whether guess1 == randomOne or if randomTwo is truthy or if randomThree is truthy. It does not check if guess1 == randomOne or guess1 == randomTwo or guess1 == randomThree as intended. You can fix this by changing the condition to:

if guess1 in [randomOne, randomTwo, randomThree]:

to check if guess1 is equal to either of the three random variables.

Edit:

There is also an issue in the try/except block. If the user enters a non-digit character in the input, a ValueError will be raised before guess is defined. But then guess is referenced in the except block, but guess isn't defined at that point.

You can fix this by getting the user input and then, separately, trying to convert the input to an int.

Here's an example:

while True:
    guess = input('Enter a number between 1 and 20: ')
    try:
        guess = int(guess)
        if 1 <= guess <= 20:
            return guess
    except ValueError:
        print (guess, 'is not a valid guess!')
    break
Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • I see the problem with the conditionals, I'm always mixing up my greater than's/less than's for some reason (should be relatively simple, think of pac man, that's how it was explained to me years back). The input validation is still a pain, though, what's wrong with my while true: block? – deHart Oct 28 '18 at 18:48