-5

我希望我的程序询问用户是否想再次使用它(即重新启动)。我如何在 Python 3.3 中做到这一点?

我试过这个

loop=1
while(loop==1):



    #code



loop-=1
    done=0
    while(done==0):
       choice=input("Do you want to restart?(Y/N)")
       choice1=choice.upper()
       if(choice1=="Y"):
           loop+=1
           done=1
           print("Restarting...")
       elif(choice1=="N"):
           done=1
           print("The program will now END. Thank you for using the program.")
4

2 回答 2

3
while 1:
    main()
    if input('Continue? [y/n]') == 'n':  # Ideally you would check they actually entered y or n
        break
于 2013-09-01T11:47:38.783 回答
0

看来您正在寻找类似do...whilePython 中不存在的东西。

可以像这样构造此构造的替换(“至少运行一次,重复直到满足条件”):

abort = False
while not abort:
    # do stuff
    exit = input("Exit [Y/N]? ").lower()
    if (exit ==  "y"):
        abort = True
于 2013-09-01T11:47:18.920 回答