3
votes

Implement the GuessNumber game. In this game, the computer - Think of a random number in the range 0-50. (Hint: use the random module.) - Repeatedly prompt the user to guess the mystery number. - If the guess is correct, congratulate the user for winning. If the guess is incorrect, let the user know if the guess is too high or too low. - After 5 incorrect guesses, tell the user the right answer.

The following is an example of correct input and output.

I’m thinking of a number in the range 0-50. You have five tries to
guess it.
Guess 1? 32
32 is too high
Guess 2? 18
18 is too low
Guess 3? 24
You are right! I was thinking of 24!

This is what I got so far:

import random

randomNumber = random.randrange(0,50)
print("I’m thinking of a number in the range 0-50. You have five tries to guess it.")
guessed = False

while guessed == False:
    userInput = int(input("Guess 1?"))
    if userInput == randomNumber:
        guessed = True
        print("You are right! I was thinking of" + randomNumber + "!")
    elif userInput>randomNumber:
        print(randomNumber + "is too high.")
    elif userInput < randomNumber:
        print(randomNumber + "is too low.")
    elif userInput > 5:
        print("Your guess is incorrect. The right answer is" + randomNumber)

print("End of program")

I've been getting a syntax error and I don't know how to make the guess increase by one when the user inputs the wrong answer like, Guess 1?, Guess 2?, Guess 3?, Guess 4?, Guess 5?, etc...

3

3 Answers

2
votes

Since you know how many times you're going through the loop, and want to count them, use a for loop to control that part.

for guess_num in range(1, 6):
    userInput = int(input("Guess " + str(guess_num) + "?"))
    if userInput == randomNumber:
        # insert "winner" logic here
        break
    # insert "still didn't guess it" logic here

Do you see how that works?

2
votes

You forgot to indent the code that belongs in your while loop. Also, you want to keep track of how many times you guessed, with a variable or a loop as suggested. Also, when giving a hint you probably want to print the number guessed by the player, not the actual one. E.g.,

import random

randomNumber = random.randrange(0,50)
print("I’m thinking of a number in the range 0-50. You have five tries to guess it.")
guessed = False
count = 0

while guessed is False and count < 5:
    userInput = int(input("Guess 1?"))
    count += 1
    if userInput == randomNumber:
        guessed = True
        print("You are right! I was thinking of" + randomNumber + "!")
    elif userInput > randomNumber:
        print(str(userInput) + " is too high.")
    elif userInput < randomNumber:
        print(str(userInput) + " is too low.")
    if count == 5:
        print("Your guess is incorrect. The right answer is" + str(randomNumber))
        print("End of program")
1
votes

You are facing the syntax error because you are attempting to add an integer to a string. This is not possible. To do what you want you need to convert randomNumber in each print statement.

import random

randomNumber = random.randrange(0,50)

print("I’m thinking of a number in the range 0-50. You have five tries to guess it.")
guessed = False

while guessed == False:
    userInput = int(input("Guess 1?"))
    if userInput == randomNumber:
        guessed = True
        print("You are right! I was thinking of" + str(randomNumber) + "!")
    elif userInput>randomNumber:
        print(str(randomNumber) + "is too high.")
    elif userInput < randomNumber:
        print(str(randomNumber) + "is too low.")
    elif userInput > 5:
        print("Your guess is incorrect. The right answer is" + randomNumber)

print("End of program")