“quit”命令在python中不起作用

2024-10-01 17:27:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是当我问玩家是否想再玩一次,如果他们说不,我就把它放到了应该退出脚本的地方。但似乎没用。以下是完整代码:

def choose_level():

    print("Please choose a level:\n"
          "\n"
          "(Level 1) - Easy\n"
          "(Level 2) - Medium\n"
          "(Level 3) - Hard\n")

    lvl_1={"Level 1","level 1","1"}
    lvl_2={"Level 2","level 2","2"}
    lvl_3={"Level 3","level 3","3"}

    choice=input("")
    if choice in lvl_1:
        level_1()
    elif choice in lvl_2:
        level_2()
    else:
        print ("[!] UNKNOWN LEVEL [!]\n")
        choose_level()

def level_1():

    import random #imports the module 'random'

    yes={"Yes","Y","yes","y"} #set 'yes' to these 4 strings
    no={"No","N","no","n"} #set 'no' to these 4 strings

    name=input("What's your name?\n") #asks for a name

    secret=int(random.random()*10)+1 #randomly generate a number

    trynum=0 #sets the number of tries to 0

    print ("---------------------------------------------------------------------\n"
           "---- Welcome,",name+"! To Level 1!\n"
           "---- I am thinking of a number between 1 and 10.\n"
           "---- Lets see how many times it will take you to guess the number.\n"
           "---------------------------------------------------------------------\n")

    while True: #starts a loop

        trynum=trynum+1 #sets number of tries to itself + 1

        guess=int(input("What is guess #"+str(trynum)+"?\n")) #asks for a guess

        if guess<secret: #if guess is too low/less than(<)
            print ("Too Low. Try again!")

        elif guess>secret: #if guess is too high/greater than(>)
            print ("Oops!! Too high, better luck next try!")

        else: 
            if trynum==1: #if number of tries is only 1
                print ("-------------------------------------------------\n"
                       "----",name+"! You got it in",trynum,"guess!\n"
                       "-------------------------------------------------\n")
            else: #if number of tries is anything else
                print ("-------------------------------------------------\n"
                       "----",name+"! You got it in",trynum,"guesses!\n"
                       "-------------------------------------------------\n")

            if trynum<3: #if guessed in less than 3 tries
                print ("Bravo!")

            elif trynum<5: #if guessed in less than 5 tries
                print ("Hmmmmpf... Better try again")

            elif trynum<7: #if guessed in less than 7 tries
                print ("Better find something else to do!")

            else: #if guessed in more than 7 tries
                print ("You should be embarssed! My dog could pick better.")

            choice=input("""Would you like to play again? ("yes" or "no")\n""") #asks if you want to play again
            if choice in yes: #if yes, then run game again
                choose_level()
            if choice in no: #if no, then quit the game
                print ("Okay.. Goodbye :(")
                quit
            break

def level_2():

    import random

    yes={"Yes","yes","Y","y"}
    no={"No","no","N","n"}

    name=input("What's your name?\n")

    secret=int(random.random()*8)+1

    trynum=0

    print ("--------------------------------------------------------------------------\n"
           "---- Welcome,",name+"! to Level 2!\n"
           "---- I am thinking of a number between 1 and 8.\n"
           "---- You have 20 guesses, and the number changes after every 4th guess\n"
           "--------------------------------------------------------------------------\n")

    while True:

        trynum=trynum+1

        guess=int(input("What is guess #"+str(trynum)+"?\n"))

        if trynum==4:
            secret=int(random.random()*8)+1
            print ("The number has changed!!")

        elif trynum==8:
            secret=int(random.random()*8)+1
            print ("The number has changed!!")

        elif trynum==12:
            secret=int(random.random()*8)+1
            print ("The number has changed!!")

        elif trynum==16:
            secret=int(random.random()*8)+1
            print ("The number has changed!!")

        elif trynum==20:
            print ("-------------------------------------------------\n"
                   "----",name+", you lost! :(\n"
                   "-------------------------------------------------\n")
            choice=input("""Would you like to play again? ("yes" or "no")\n""") #asks if you want to play again
            if choice in yes: #if yes, then run game again
                choose_level()
            elif choice in no: #if no, then quit the game
                print ("Okay.. Goodbye :(")
                quit

        if guess<secret:
            print ("Too low, try again!")

        elif guess>secret:
            print ("Too high, try again!")

        else: 
            if trynum==1: #if number of tries is only 1
                print ("-------------------------------------------------\n"
                       "----",name+"! You got it in",trynum,"guess!\n"
                       "-------------------------------------------------\n")

            else: #if number of tries is anything else
                print ("-------------------------------------------------\n"
                       "----",name+"! You got it in",trynum,"guesses!\n"
                       "-------------------------------------------------\n")

            if trynum<3: #if guessed in less than 3 tries
                print ("Bravo!")

            elif trynum<5: #if guessed in less than 5 tries
                print ("Hmmmmpf... Better try again")

            elif trynum<7: #if guessed in less than 7 tries
                print ("Better find something else to do!")

            else: #if guessed in more than 7 tries
                print ("You should be embarssed! My dog could pick better.")


            choice=input("""Would you like to play again? ("yes" or "no")\n""") #asks if you want to play again
            if choice in yes: #if yes, then run game again
                choose_level()
            if choice in no: #if no, then quit the game
                print ("Okay.. Goodbye :(")
                quit
            break
choose_level()
level_1()
level_2()

我的问题是:

^{pr2}$

当我运行这个程序,到了那个时候,输入‘no’,我就回到了第一级。我被卡住了,不知道该怎么办。感谢任何帮助。在


Tags: tonoinnumberifrandomlevelyes
2条回答

在系统出口()、exit()、quit()和os。\ exit(0)终止Python解释器 应该使用quit(),而不是quit

您缺少quit命令后面的一对括号。quit命令调用一个函数,就像choose\u level一样,因此需要告诉Python要传入哪些参数。每个函数调用都需要一对括号,告诉Python需要什么参数。quit函数也一样。在本例中,没有参数,所以只需键入quit()。在

相关问题 更多 >

    热门问题