Home > Blockchain >  My while loop keeps repeating even when its suppose to reach the limit
My while loop keeps repeating even when its suppose to reach the limit

Time:08-06

I'm new to python, and for some reason my while loop in def hint system still repeats when it reaches the limit. Here's the code to my "guessing game."

print ("guessing game 1 - 10\n\n")
import random
number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

random.shuffle (number)
result = (number [0] )
secret_number = (result)
guess_count = 0
guess_limit = 3

def hint_system ():
    hint_count = 0
    hint_limit = 1
    while hint_count < hint_limit:
        hint_request = input('Would you like to use your hint?, Yes or no?: ')  
        hint_count  = 1
        if guess > secret_number:
            print("lower")
        elif guess < secret_number:
            print ("higher")
            break
        else:
            print ("Hint not used") 
    else:
        print("\n\n")
        print("Hint used up")
        
while guess_count < guess_limit:
    guess = int(input( "Guess: "))
    guess_count  = 1
    hint_system ( )
    if guess == secret_number:
        print("congrats you win!")
        break
    else:
        print("You failed!")
        print("correct answer: "  str(secret_number))

The result is

guessing game 1 - 10

Guess: 2 Would you like to use your hint?, Yes or no?: yes

higher

Guess: 2 Would you like to use your hint?, Yes or no?:

CodePudding user response:

When you use break, the else clause after the while doesn't get executed.

See this post for more.

CodePudding user response:

You keep resetting the hint function so the hint count keeps going to 0, so it will continue indefinitely.

It is best to keep the hint count variable as a variable in the function:

def func(hint_count):
  ...

CodePudding user response:

The logic of the game didn't make much sense to me so I reworked it as follows. It generates a secret, and you get 3 guesses. If the guess is wrong then you are asked if you want a hint:

import random
import sys


def hint_system(secret):
    if guess > secret:
        print("lower")
    elif guess < secret:
        print("higher")


guess_range = (1, 10)
guess_attempts = 3
print(f"guessing game {guess_range[0]} to {guess_range[1]}\n")

secret = random.randint(guess_range[0], guess_range[1])
for attempt in range(guess_attempts):
    guess = int(input("Guess: "))
    if guess == secret:
        print("congrats you win!")
        sys.exit()
    else:
        print("Sorry. This is not correct.")
        if input('Would you like to use your hint (yes/no)? ') != 'no':
            hint_system(secret)

print(f"You failed! The correct answer was {secret}")

and here is an example session:

guessing game 1 to 10

Guess: 5
Sorry. This is not correct.
Would you like to use your hint (yes/no)? yes
higher
Guess: 8
Sorry. This is not correct.
Would you like to use your hint (yes/no)? no
Guess: 9
Sorry. This is not correct.
Would you like to use your hint (yes/no)? yes
lower
You failed! The correct answer was 6
  • Related