Michael Acosta
Michael Acosta

Reputation: 3

Guess My number game with limited number of guesses

I'm new to python and I'm trying to make the guess my number game with a limit of only 5 guesses, everything I've tried so far has failed. how can I do it?, I forgot to mention that I wanted the program to display a message when the player uses all their guesses.The code below only prints the "You guessed it" part after the 5 guesses whether they guess it or not.

import random
print ("welcome to the guess my number hardcore edition ")

print ("In this program you only get 5 guesses\n")

print ("good luck")

the_number = random.randint(1, 100)

user = int(input("What's the number?"))

count = 1
while user != the_number:

    if user > the_number:
     print ("Lower")

    elif user < the_number:
     print ("Higher")

    user = int(input("What's the number?"))
    count += 1
    if count == 5:
     break


print("You guessed it!!, the number is", the_number, "and it only"\
       " took you", count , "tries")

input ("\nPress enter to exit")             

Upvotes: 0

Views: 15102

Answers (2)

lvc
lvc

Reputation: 35089

Your edit says you want to differentiate between whether the loop ended because the user guessed right, or because they ran out of guesses. This amounts to detecting whether you exited the while loop because its condition tested false (they guessed the number), or because you hit a break (which you do if they run out of guesses). You can do that using the else: clause on a loop, which triggers after the loop ends if and only if you didn't hit a break. You can print something only in the case you do break by putting the print logic right before the break, in the same conditional. That gives you this:

while user != the_number:
    ...
    if count == 5:
        print("You ran out of guesses")
        break
else:
    print("You guessed it!!, the number is", the_number, "and it only"\
          " took you", count , "tries")

However, this puts code for different things all over the place. It would be better to group the logic for "guessed right" with the logic for warmer/colder, rather than interleaving them with part of the logic for how many guesses. You can do this by swapping where you test for things - put the 'is it right' logic in the same if as the warmer/colder, and put the number of guesses logic in the loop condition (which is then better expressed as a for loop). So you have:

for count in range(5):
    user = int(input("What's the number?"))
    if user > the_number:
        print("Lower")
    elif user < the_number:
        print("Higher")
    else:
        print("You guessed it!!, the number is", the_number, "and it only"\
              " took you", count , "tries")
        break
else:
    print("You ran out of guesses")

Upvotes: 2

nico
nico

Reputation: 2121

You have two options: you can either break out of the loop once the counter reaches a certain amount or use or a for loop. The first option is simplest given your code:

count = 0
while user != the_number:

    if user > the_number:
     print ("Lower")

    elif user < the_number:
     print ("Higher")

    user = int(input("What's the number?"))
    count += 1
    if count == 5: # change this number to change the number of guesses
        break # exit this loop when the above condition is met

Upvotes: 1

Related Questions