我的问题在于,当我询问玩家是否想要再次玩游戏时,如果他们拒绝,我会把它放到它应该退出剧本的地方。但它似乎没有起作用。这是完整的代码:
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()
我遇到的问题是:
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
当我运行程序时,我到达那一点,然后键入“不”#39;它只是让我回到了第一级。我被卡住了,不知道从哪里开始。任何和所有的帮助表示赞赏。
答案 0 :(得分:1)
您缺少quit命令后的括号对。 quit命令就像choose_level一样调用一个函数,所以你需要告诉Python你要传入哪些参数。每个函数调用都需要一对括号,告诉Python你想要什么参数。退出功能也不例外。在这种情况下,将没有参数,因此您只需键入quit()。
答案 1 :(得分:0)
sys.exit(),exit(),quit()和os._exit(0)杀掉Python解释器 你应该使用quit(),而不是退出