即使我试图“结束”代码,Python代码也会不断重复输入值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了即使我试图“结束”代码,Python代码也会不断重复输入值相关的知识,希望对你有一定的参考价值。
这是我的代码,如果你玩它,那么你应该看到什么是错的。一旦玩家死亡或获胜,我不确定如何结束代码。播放到结尾并查看代码,以便了解可能存在的问题。
非常感谢,这是代码:
def start ():
print ("Hello! Welcome to Prison Break!")
print (""""You are a very infamous spy working with a foreign government in the US. You are always successful and have never been caught...until now.""")
print ("You were captured by the FBI when you were walking down the street after a mission, and they jumped you.")
print ("You don't know what is happening!")
description_of_room()
def description_of_room ():
print ("You wake up in a prison cell. Everything seems very old, dust lies on the ground, untouched for years.")
print ("The room seems empty besides the cell bars, crumbling wall behind you, and a pile of rubble.")
room_1()
def room_1 ():
print("")
prompt_0()
def prompt_0 ():
prompt_0 = input("What do you do?.")
try:
if prompt_0 == "look":
print(" ")
description_of_room() # goes internally into room_1
if prompt_0 == "cell bars":
print("You examine the cell bars carefully.")
print("The bars are very old, a wrench could be able to rip them open")
room_1()
if prompt_0 == "rubble":
print("You closely examine the rubble. You see a metal rusty handle of something.")
print("You pull it easily out of the rubble, its a old wrench, maybe it could be used on the cell bars.")
options()
elif prompt_0 == "wall":
print("You examine the crumbling wall.")
print("It is extremely old, it seems someone has cut away at it, making it delicate, a hammer could easily take care of it.")
room_1()
else:
print("Please type in cell bars, wall, rubble, or look")
room_1()
except ValueError():
print("ARE YOU CRAZY? DON'T TYPE NUMBERS! YOU'LL CRASH MY GAME!")
print(" ")
room_1()
room_1()
options()
def options():
print()
prompt_1()
def prompt_1():
prompt_1 = input("Use wrench on bars or trade with Billy for a Hammer, who is next door.")
try:
if prompt_1 == "use wrench":
print("The wrench will be able to break open the cell bars.")
print("If you do it now you will be caught, you must wait till night.")
escape_2_mor()
if prompt_1 == "trade with billy":
print("Billy is the common trader in the prison, he will take pretty much anything.")
print("You give the rusty wrench to Billy and he gives you a decent small hammer, which can be used to smash the crumbly wall.")
failure()
elif prompt_1 == "Random":
print("RANDOM")
options()
else:
print("Type trade with billy or use on cell bars.")
options()
except ValueError():
print("NO NUMBERS!")
print(" ")
options()
def escape_2_mor():
print()
prompt_3()
def prompt_3():
prompt_3 = input("What do you do?")
try:
if prompt_3 == "wait":
print("You wait till night.")
escape_2_nih()
elif prompt_3 == "use wrench":
print("You were caught by the police, punishment is death.")
print("You died, Game Over!")
end()
else:
print("Type wait or use wrench")
escape_2_mor()
except ValueError():
print("I SAID NO NUMBERS")
print(" ")
escape_2_mor()
def escape_2_nih():
print()
prompt_4()
def prompt_4():
prompt_4 = input("It's midnight, you have the chance to use the wrench!")
try:
if prompt_4 == "use wrench":
print("You slowly break the old bars, the noise echoing throughout the halls.")
escape_2_nih_1()
elif prompt_4 == "wait":
print("You wait for no reason, it's morning again.")
escape_2_mor()
else:
print("Type use wrench or wait")
except ValueError:
print("Why do I even try...")
print(" ")
escape_2_nih()
def escape_2_nih_1():
print()
prompt_5()
def prompt_5():
prompt_5 = input("Your outside your cell bars, in a dark hallway, what do you do?")
try:
if prompt_5 == "crawl":
print("You get on your knees quietly, making sure no one can see you.")
print("You stick in the shadows and come across a guard.")
print("You hit him on the head with the wrench, luckily he is wearing a helmet, so he is just knocked out.")
escape_2_des
elif prompt_5 == "run":
print("You run quickly down the hallway, you see other cellmates sleeping.")
print("You almost reach the metal door at the end of the hallway, but a guard turns and sees you.")
print("He is so suprised that he fires his gun before you can attack him.")
print("You fall to the ground, everything goes dark, you died.")
end()
else:
print("Please type crawl or run")
escape_2_nih_1()
except ValueError:
print("Just stop")
escape_2_nih_1()
escape_2_des()
def escape_2_des():
print()
prompt_6()
def prompt_6():
prompt_6 = input("You press open the door and you see towering gates and a guard tower, what do you do?")
try:
if prompt_6 == "tower":
print("You see the tower, and slowly approach it. You reach the tower and see its made out of stone that can be easily climbed.")
print("You climb up the wall, and then reach the top. You can see that the tower is high enough to jump over the fence and escape.")
print("You take a huge leap and fall to the ground, you look behind you and see the fence and the prison.")
print("You have escaped the prison! You Win!")
end()
if prompt_6 == "fence":
print("You slowly approach the fence and put your hand on it to start climbing.")
print("You are quickly electrocuted by the electric fence, stopping your heart.")
print("You've lost!")
end()
else:
print("Type fence or tower")
escape_2_des()
except ValueError:
print("Your annoying")
escape_2_des()
def failure():
print()
prompt_7()
def prompt_7():
prompt_7 = input("You can now break open the crumbled wall")
try:
if prompt_7 == "break wall":
print("You hammer away at the wall, which is breaking down easily.")
print("All the sudden a guard comes up from behind and stares down at you.")
print("Hello Officer! How are you doing today?")
print("Please come with me inmate.")
print("He pulls you by the shoulders and walks you down into a different cell, a more clean and sturdy cell.")
print("He pushes you in and locks the cell bars behind you, you have failed and will never get out.")
end()
if prompt_7 == "wait":
print("You wait for no reason, good job!")
failure()
else:
print("Type wait or break wall")
except ValueError:
print("Whatever")
failure()
end()
def end():
print("Congrats you have either won or failed the game. I hope you enjoyed!")
print(" ")
print("Ignore the statement underneath, the code is being an idiot")
start()
如果有人能提供帮助,那将是一个巨大的帮助,谢谢。我不确定这是输入是问题还是什么,但它不断重复。你可以使用pycharm,因为这对我有用,如果它不适用于你可能用来运行python的任何其他程序,那么只是尝试请使用Pycharm,因为我知道它有效。
答案
您的end
函数实际上并不会停止代码执行。你需要做的就是这个
import sys
#your code
#your code
#your code
def end():
print("Congrats you have either won or failed the game. I hope you enjoyed!")
print(" ")
print("Ignore the statement underneath, the code is being an idiot")
sys.exit()
以上是关于即使我试图“结束”代码,Python代码也会不断重复输入值的主要内容,如果未能解决你的问题,请参考以下文章