Guessing Projects How to add guess limit Python

Viewed 64
import random

real_num = random.randrange(1,50)
guess = int(input("Enter a number between 1 to 50: "))

while guess != real_num:
    if guess < real_num:
        print("Guess higher")
        guess = int(input("Enter a number between 1 to 50: "))
    else:
        print("Guess lower")
        guess = int(input("Enter a number between 1 to 50: "))
print("YOU WIN")

Hi, I would like to add a limit for this game and I have tried several attempts but the results does not seems not be working. Thank you for the help. Appreciate it.

5 Answers

Really all you need is a counter with each guess

import random

real_num = random.randrange(1,50)
guess = int(input("Enter a number between 1 to 50: "))
attempts = 0 #

while guess != real_num and attempts < 10: # checking if attempts is less than the max (10)
    if guess < real_num:
        print("Guess higher")
        guess = int(input("Enter a number between 1 to 50: "))
        attempts+=1
    elif guess == real_num:  # checking if the guess is equal to the answer
        print("YOU WIN")
        break               # break to stop the game
    else:
        print("Guess lower")
        guess = int(input("Enter a number between 1 to 50: "))
        attempts+=1

All we are doing here is checking if we are still under the max attempts, and if the attempt is wrong we add one to the attempts.

You may set the total number of allowed attempts in a variable. For each guess, decrement the value of that variable by one.

import random


real_num = random.randrange(1, 50)
total_attempts = 3
while True:
    guess = int(input("Enter a number between 1 to 50: "))
    if guess == real_num:
        break
    total_attempts -= 1
    if total_attempts == 0:
        break
    if guess < real_num:
        print("Guess higher")        
    else:
        print("Guess lower")    

if total_attempts != 0:
    print("YOU WIN")
else:
    print("YOU LOST")

Output:

Enter a number between 1 to 50: 23
Guess lower
Enter a number between 1 to 50: 12
Guess lower
Enter a number between 1 to 50: 4
YOU LOST

You can do like this.

import random

real_num = random.randrange(1,50)
guess = int(input("Enter a number between 1 to 50: "))
guess_limit = 10

while guess != real_num:
    if guess_limit == 0:
        print("Guess limit reached")
        exit(0)
    
    if guess < real_num:
        print("Guess higher")
        guess = int(input("Enter a number between 1 to 50: "))
    else:
        print("Guess lower")
        guess = int(input("Enter a number between 1 to 50: "))

    guess_limit -= 1


print("YOU WIN")

You can add a variable to track how many attempts have been tried and just add that as a condition to break out of the while loop. You can also set a limit variable to check the guess count against each iteration.

Edit: you also want to make sure when breaking out of the loop if the user went over the limit -- my initial answer didn't take this into account so it would've said "Too many guesses! YOU WIN"

Edited snippet:

import random

real_num = random.randrange(1,50)
guess = int(input("Enter a number between 1 to 50: "))

guess_count = 0 # keep track of guesses
guess_limit = 10 # set to whatever you want

while guess != real_num:
    # check first if there were too many guesses
    if guess_count >= guess_limit:
        print("Too many guesses!")
        break

    # otherwise carry on with regular game logic
    if guess < real_num:
        print("Guess higher")
        guess = int(input("Enter a number between 1 to 50: "))
    else:
        print("Guess lower")
        guess = int(input("Enter a number between 1 to 50: "))

    # if we reach this point, it was an incorrect guess
    # increment guess count here
    guess_count += 1

# check to see if the user guessed too many times
if guess_count >= guess_limit:
    print("YOU LOSE")
else:
    print("YOU WIN")

You can use this method

import random

limit=5
is_win=Fasle
real_num = random.randrange(1,50)
guess = int(input("Enter a number between 1 to 50: "))
   
for i in range(1,limit):
    if guess==real_num:
        print("YOU WIN")
        is_win=True
    elif guess<real_num:
        print("Guess higher | Number of times remaining :",(limit-i))
        guess = int(input("Enter a number between 1 to 50: "))
    elif guess>real_num:
        print("Guess lower | Number of times remaining :",(limit-i))
        guess = int(input("Enter a number between 1 to 50: "))

if is_win==False:
    print("Unfortunately you did not win")

output :

Enter a number between 1 to 50: 10                                                                                              
Guess higher | Number of times remaining : 4
...
Related