I am a beginner firstly. I just wrote this simple code but I have one problem. Whenever you guess the wrong number once and try to stop playing, it just doesn't respond to you and continues whatever you write. If you want you can try the code and see what I am talking about.
import random
def main() :
while True :
if True :
try :
numbers = int(input("Choose a positive number : "))
mine = random.randint(0, numbers)
except ValueError :
print ("Write correctly please.")
continue
print ("I've chosen a number between 0 and " + str(numbers) + ".")
guess = int(input("Take a guess : "))
if guess == mine :
print ("Nice guess !")
choice = input ("Play agian (y/n) ? ")
if choice == "y" :
main()
if choice == "n" :
break
else :
print ("Sorry, wrong guess.")
print ("I choose " + str(mine) + ".")
choice2 = input ("Try agian (y/n) ? ")
if choice2 == "y" :
main()
if choice2 == "n" :
break
main ()
I tried using continue and pass and some other staff, but I don't understand functions and loops associated that much, so I would appreciate anyone also to give me some kind of video or course to train these codes.
The issue you are having is that your code is calling the same function inside itself. This creates a situation where your game doesn't stop properly when you want to finish it. To solve this, you can organize your code in a way that uses a loop to control your game. This way, when you want to stop playing the game, you can say "I am done" and the game will stop.
Loops can be very tricky, so I have modified your code: