'quit' command not working in python
我的问题是,当我问玩家是否想再玩一次,如果他们说不,我就把它带到了应该退出剧本的地方。但它似乎不起作用。完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | def choose_level(): print("Please choose a level: " " " "(Level 1) - Easy " "(Level 2) - Medium " "(Level 3) - Hard ") 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 [!] ") 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? ") #asks for a name secret=int(random.random()*10)+1 #randomly generate a number trynum=0 #sets the number of tries to 0 print ("--------------------------------------------------------------------- " "---- Welcome,",name+"! To Level 1! " "---- I am thinking of a number between 1 and 10. " "---- Lets see how many times it will take you to guess the number. " "--------------------------------------------------------------------- ") while True: #starts a loop trynum=trynum+1 #sets number of tries to itself + 1 guess=int(input("What is guess #"+str(trynum)+"? ")) #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 ("------------------------------------------------- " "----",name+"! You got it in",trynum,"guess! " "------------------------------------------------- ") else: #if number of tries is anything else print ("------------------------------------------------- " "----",name+"! You got it in",trynum,"guesses! " "------------------------------------------------- ") 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") """) #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? ") secret=int(random.random()*8)+1 trynum=0 print ("-------------------------------------------------------------------------- " "---- Welcome,",name+"! to Level 2! " "---- I am thinking of a number between 1 and 8. " "---- You have 20 guesses, and the number changes after every 4th guess " "-------------------------------------------------------------------------- ") while True: trynum=trynum+1 guess=int(input("What is guess #"+str(trynum)+"? ")) 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 ("------------------------------------------------- " "----",name+", you lost! :( " "------------------------------------------------- ") choice=input("""Would you like to play again? ("yes" or"no") """) #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 ("------------------------------------------------- " "----",name+"! You got it in",trynum,"guess! " "------------------------------------------------- ") else: #if number of tries is anything else print ("------------------------------------------------- " "----",name+"! You got it in",trynum,"guesses! " "------------------------------------------------- ") 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") """) #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() |
我遇到的问题是:
1 2 3 4 5 6 7 8 | choice=input("""Would you like to play again? ("yes" or"no") """) #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 |
号
当我运行程序时,我到达了那个点,输入"no",它只会让我回到第一层。我被卡住了,不知道从这里到哪里去。感谢您的帮助。
您缺少退出命令后出现的一对括号。quit命令像choose_level一样调用函数,因此您需要告诉python您想要传递什么参数。每个函数调用都需要一对括号来告诉python需要什么参数。退出功能没有什么不同。在这种情况下,将没有参数,因此只需键入quit()。
sys.exit()、exit()、quit()和os.u exit(0)终止python解释器您应该使用quit(),而不是quit