python中quit函数⽤法_“quit”命令在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"}
random在python中的意思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
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 (" 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
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
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 (" 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’,我就回到了第⼀级。我被卡住了,不知道该怎么办。感谢任何帮助。在

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。