I'm currently teaching myself python from books, YouTube, etc. Recently, I've started from Python Tutorial - Python for Beginners [Full Course]. I've been trying to make some bigger programs by myself (learning from python documentation) than the ones in this video. Hence, this is my question:
I'm trying to find a solution for how to exit from a function the_func
after pressing keys (n, q ,e)
. Unfortunately, after choosing an option to not try again, the loop is still running. This is the output copied from the terminal:
Guess the secret number: 1
Guess the secret number: 2
Guess the secret number: 3
Sorry, you are loser!
-------------------------
Would you tray again? Y/N
y
Guess the secret number: 9
The secret number was 9.
You are won!
-------------------------
Would you tray again? Y/N
n
Thank you!
Guess the secret number:
You can see that even after choosing n
, it's starting again.
This is the code:
# Guessing Game
def the_func() :
again = 'Would you tray again? Y/N'
scecret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
guess = int(input("Guess the secret number: "))
guess_count = 1
if guess == scecret_number:
print(f"The secret number was {scecret_number}.\nYou are won!")
break
else:
print("Sorry, you are loser!")
print("-" * int(len(again)), "\n")
print(again)
answer = input(" ")
while True:
if answer.lower() == "y":
the_func()
#elif answer.lower() == "n":
if answer.lower() in {'n', 'q', 'e'}:
print("Thank you!")
#return
break
# else:
# print("Sorry, that was an invalid command!")
the_func()
the_func()
Any help is appreciated.
CodePudding user response:
The problem you are experiencing is known as Recursion. In simple words, the function is getting called repeatedly at the end of it. Here's the example that might clear it more to you:
def foo()
while True:
print('xyz')
break
foo() # Calling the function at the end causes recursion, i.e., infinite looping of the function (unless 'it is returned before the call')
foo()
You might ask, what is the solution? As said above, you can prevent infinite recursion of a function (if you want to), by implicit returning the function using return.
Your answer is present in your code only. Let me show you where:
while True:
if answer.lower() == "y":
the_func()
#elif answer.lower() == "n":
if answer.lower() in {'n', 'q', 'e'}: # I would use 'elif' here, if the above 'if' statement did not have the calling of the same function. But, here, using 'if' or 'elif' doesn't matter really.
print("Thank you!")
return
#break
#else:
#print("Sorry, that was an invalid command!")
I hope you understood what you are doing wrong, and if you have any doubts, just add a comment and I would help as fast as I can.
CodePudding user response:
Haaa.. DONE, So what we need
while True:
if answer.lower() == "y":
return the_func() ***# <= add return to the_func()***
if answer.lower() in {'n', 'q', 'quit', 'e', 'exit'}:
print("Thank you!")
return
#the_func() #<== mark as comment, and func not will bee looped again the_func()
the_func()
However, @Alex thank you!