H. Soto
H. Soto

Reputation: 49

While loops, if-else statements: Guess my number game

I'm a beginner using python, and am writing a "guess my number game". So far I have everything working fine. The computer picks a random number between 1 and 3 and asks the player to guess the number. If the guess is higher than the random number, the program prints "Lower", and vice versa. The player only has 5 tries, and when they run out, the player gets a message and the game ends. If the player guesses correctly, they are congratulated and the game ends. However, sometimes when the number is guessed correctly, the program doesn't print the congratulatory message and I can't figure out why...

import random

print("\tWelcome to 'Guess My Number'!:")
print("\nI'm thinking of a numer between 1 and 100.")
print("Guess carefully, you only have 5 tries!.\n")

#sets initial values
the_number = random.randint(1,3)
guess = int(input("Take a guess: "))
tries = 1
guesses = 4


#guessing loop
while guess != the_number:
    if guess > the_number:
        print("Lower...")
    elif guesses <= 0:
        print("Sorry, you're out of guesses! Try again...")
        break

    elif guess < the_number:
        print("Higher...")



    guess = int(input("Take a guess: "))
    tries += 1
    guesses -= 1

    if guess == the_number:
        print("You guessed it! The number was", the_number)
        print("And it only took you", tries, "tries!\n")

Upvotes: 1

Views: 4750

Answers (3)

Heleen Lindeque
Heleen Lindeque

Reputation: 11

To answer your original question about the lack of congratulatory message for correct number, end the code with input(), to ensure it does not terminate before displaying the last message.

Order of calculation:

  1. give input guess
  2. reduce guesses (starting at 5), increase tries (starting at 1)
  3. immediate break if guesses == 0
  4. evaluate guess (lower, higher or equal, which would end while loop)

import random

print("\tWelcome to 'Guess My Number'!:")
print("\nI'm thinking of a numer between 1 and 3.")
print("Guess carefully, you only have 5 tries!.\n")

#sets initial values
the_number = random.randint(1,3)
guess = int(input("Take a guess: "))
tries = 1
guesses = 5


#guessing loop


while guess != the_number:
    tries += 1
    guesses -= 1

    if guesses == 0:
        print("Sorry, you're out of guesses! Try again...")
        break

    elif guess > the_number:
        print("Lower...")
    elif guess < the_number:
        print("Higher...")

    

    guess = int(input("Take a guess: "))

if guess == the_number:
    print("You guessed it! The number was", the_number)
    print("And it only took you", tries, "tries!\n")

input()

Upvotes: 1

lucasnadalutti
lucasnadalutti

Reputation: 5948

If you guess the number in your first try, the program will require another guess nevertheless since

guess = int(input("Take a guess: "))
tries += 1
guesses -= 1

comes before

if guess == the_number:
    print("You guessed it! The number was", the_number)
    print("And it only took you", tries, "tries!\n")

when you already asked for a guess outside the loop. You should only ask for input inside the loop:

the_number = random.randint(1,3)
tries = 0
guesses = 5

#guessing loop
guess = None
while guess != the_number:
    guess = int(input("Take a guess: "))
    tries += 1
    guesses -= 1

    if guess > the_number:
        print("Lower...")
    elif guess < the_number:
        print("Higher...")

    if guess == the_number:
        print("You guessed it! The number was", the_number)
        print("And it only took you", tries, "tries!\n")

    if guesses <= 0:
        print("Sorry, you're out of guesses! Try again...")
        break

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191681

Assuming everything else works, un-indent the final check. You can't check guess == the_number while it isn't equal

#guessing loop
while guess != the_number:
    # do logic

# outside guessing loop
if guesses > 0:
    print("You guessed it! The number was", the_number)
    print("And it only took you", tries, "tries!\n")

Upvotes: 0

Related Questions