I think you're making a mistake common among new programmers, looking at the function definition as a sort of label you can 'go to'. That's not how it works though, a function is a sealed off bit of code that you can call when its name is known (it is "in scope", as a programmer would say).
Your code defines the function, but only calls it from within itself, there is no call outside the function that can get it started.
Also, every time a function is called, this takes up some resources, and once a function reaches the end of its body, or a return
statement, those resources are freed up and a value (or None
) returned to wherever the function was called from. You call game()
every time the player wants to play again and that could go on indefinitely, eating up some resources every time. You should instead just use a loop, like a while
.
Changing those two things:
def game():
while True:
secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < 3:
guess_count += 1
guess = int(input('Guess: '))
if guess == secret_number:
print('You got it!')
run_again = input('do you want to play again? Y/N: ')
if run_again != 'Y':
print('Bye')
# instead of exit() to terminate the script, just end the function
return
# if the code gets here, the `while True` will cause it to repeat
# this point will never be reached, either while loops, or the function returns
# calling the function here, to get it to actually execute
game()