0

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?:

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Indentation is significant in python so please format your code. Import should be first thing in your file. hint_request is not used so why ask? – Allan Wind Aug 06 '22 at 01:45
  • You ask if they want to use a hint, but then assume they said yes. But when you call `hint_system` again, the hint count (a local variable) is reset to 0. – chepner Aug 06 '22 at 01:48
  • It's a really good idea to keep variables close to where they are used. – Allan Wind Aug 06 '22 at 01:49
  • A suggestion: what happens if the user says they don't want to use a hint? `hint_request` is assigned, but never used anywhere. – Chris Aug 06 '22 at 01:55

3 Answers3

0

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

See this post for more.

puncher
  • 1,570
  • 4
  • 15
  • 38
0

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):
  ...
DialFrost
  • 1,610
  • 1
  • 8
  • 28
0

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
Allan Wind
  • 23,068
  • 5
  • 28
  • 38