简体   繁体   中英

how to ask the user if they want the code to start again

I am just starting out on a python Nat5 course so I'm sorry if the answer is glaringly obvious but what I'm trying to do is ask the user if they want the code to restart or not and ask again if the user inputs an incorrect answer

print("would you like to ask again?")
go = input("enter yes/y or no/n")

if(go == 'y') or (go == 'yes'):
#replay here
elif(go == 'n') or (go == 'no'):
    exit()
else:
print("It's a yes or no question")
#ask to replay again

I do not know if this is the best practice, but it works for me and I have had no issues. Put the code in a function

def code():
    #insert the code here

and then call the function

if (go=='y'):
   code()

It is also worth noting that you will need to also call the function at the end of the code so that it actually runs the first time round.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM