How to fix this loop or function related issue in python?

46 Views Asked by At

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.

1

There are 1 best solutions below

1
Pruthviraj Acharya On

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:

import random

def main():
    while 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) + ".")
        
        while True:
            guess = int(input("Take a guess: "))
            
            if guess == mine:
                print("Nice guess!")
                choice = input("Play again (y/n)? ")
                if choice == "y":
                    break  # Exit the inner loop and start a new game
                else:
                    return  # Exit the main function to end the game
                
            else:
                print("Sorry, wrong guess.")
                print("I chose " + str(mine) + ".")
                choice2 = input("Try again (y/n)? ")
                if choice2 == "n":
                    return  # Exit the main function to end the game

if __name__ == "__main__":
    main()