Search code examples
pythonpython-3.xrestart

How can I restart my python 3 script?


I am making a program on python 3. I have a place that I need the script to restart. How can I do this.

 #where i want to restart it
name= input("What do you want the main character to be called?")
gender = input("Are they a boy or girl?")

if gender == "boy":
    print("Lets get on with the story.")
elif gender == "girl":
    print("lets get on with the story.")
else:
    print("Sorry. You cant have that. Type boy or girl.")
    #restart the code from start

print("Press any key to exit")
input()

Solution

  • It's a general question about programming an not specific to Python ... by the way you can shorten your code with the two conditions on boy and girl...

    while True:
        name= input("What do you want the main character to be called?")
        gender = input("Are they a boy or girl?")
    
        if gender == "boy" or gender == "girl":
            print("Lets get on with the story.")
            break
    
        print("Sorry. You cant have that. Type boy or girl.")
    
    print("Press any key to exit")
    input()