Home > front end >  Automate the boring stuff "guessTheNumber" game not working as intended
Automate the boring stuff "guessTheNumber" game not working as intended

Time:12-15

This program is supposed to give the user 6 chances to guess the secret number the program is thinking of. The number is between 1 and 20 and gives different scripts depending on your answer. The problem is, its only giving me 1 chance to answer and if the answer is wrong, it will tell you what the correct answer is (which is only supposed to happen after you failed all 6 guesses) then asks you to guess again. It will keep looping this until you type the answer it gives you and then stop without giving you the proper script.

I've tried to re-organize the code with less or more spaces in between each line but nothing seems to work. I've even copied the code line for line in the link it gives to watch the example in action and I'm still getting the same problem.

# This is a guess the number game.
import random
secretNumber = random.randint(1, 20)
print("I am thinking of a number between 1 and 20.")

# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):
    print("Take a guess.")
    guess = int(input())

if guess < secretNumber:
    print("Your guess is too low.")
elif guess > secretNumber:
    print("Your guess is too high.")
else:
    break  # This condition is the correct guess!
if guess == secretNumber:
    print("Good job! You guess my number in "   str(guessesTaken)   " guesses!")
else:
    print("Nope. The number I was thinking of was "   str(secretNumber))

CodePudding user response:

Your indentation is incorrect, it needs to be inside the for loop so that it gets called 6 times:

# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):
    print("Take a guess.")
    guess = int(input())

    if guess < secretNumber:
        print("Your guess is too low.")
    elif guess > secretNumber:
        print("Your guess is too high.")
    else:
        break  # This condition is the correct guess!
    if guess == secretNumber:
        print("Good job! You guess my number in "   str(guessesTaken)   " guesses!")
    else:
        print("Nope. The number I was thinking of was "   str(secretNumber))

CodePudding user response:

The code is not properly indented in the conditional statements. See below.

# This is a guess the number game.
import random
secretNumber = random.randint(1, 20)
print("I am thinking of a number between 1 and 20.")

# Ask the player to guess 6 times.
for guessesTaken in range(6):
    print("Take a guess.")
    guess = int(input())
    # This conditions should be inside the loop, as you need to check everytime.   
    if guess < secretNumber:
        print("Your guess is too low.")
    elif guess > secretNumber:
        print("Your guess is too high.")
    else:
        break  # This condition is the correct guess!
# This conditions are outside loop, it executes after tries are exhausted or a correct guess was made
if guess == secretNumber:
    print("Good job! You guess my number in "   str(guessesTaken)   " guesses!")
else:
    print("Nope. The number I was thinking of was "   str(secretNumber))

CodePudding user response:

There is an indentation problem. Specifically the guessing conditions.

# This is a guess the number game.
import random
secretNumber = random.randint(1, 20)
print("I am thinking of a number between 1 and 20.")

# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):
    print("Take a guess.")
    guess = int(input())

    if guess < secretNumber:
        print("Your guess is too low.")
    elif guess > secretNumber:
        print("Your guess is too high.")
    else:
        break  # This condition is the correct guess!

# After successful or failed guesses, show output
if guess == secretNumber:
    print("Good job! You guess my number in "   str(guessesTaken)   " guesses!")
else:
    print("Nope. The number I was thinking of was "   str(secretNumber))
  • Related