我使用函数和随机整数在Python中创建了基于文本的冒险。我的第二个随机整数将不起作用。当我通过第一个随机整数运行代码时,它可以正常工作。但是第二个随机整数不起作用,即使它与第一个随机整数具有相同的代码,只是变量不同。我不确定是什么问题,我正在寻求帮助。
def pathChoice():
path=""
while path != "1" and path != "2":
path=input("What will Levi do? (1 or 2): ")
return pathChoice
def checkPath(pathChoice):
print(".....................................................................")
print("Levi sneaks around the side of the castle, avoiding suspecious guards.")
time.sleep(3)
print("Levi notices a small window that he could climb into.")
time.sleep(2)
print("He must scale the wet stone wall without falling.")
time.sleep(2)
correctMovement = random.randint(1,2)
if pathChoice == str(correctMovement):
print("...............................................................")
print("He scales the wall carefully, avoiding the loose rubble and climbs into the castle")
time.sleep(2)
print("Levi sneaks behind some barrels and boxes, making his way past some guards.")
time.sleep(2)
else:
print(".................................................................")
print("The wet stone wall is too slipery, and Levi falls half way up.")
time.sleep(2)
print("The loud thud of his corpse alerts the nearby guards.")
time.sleep(2)
print("Levi is apprehended by the guards.")
def castleScene():
print("....................................................................")
print("Levi is dragged to a jail cell by two guards after trying to sneak into the castle.")
time.sleep(2)
print("He feels tired and discouraged as the guards close and lock the door.")
def secondPathChoice():
pat=""
while pat !="3" and pat !="4":
path=input("What does Levi do? (3 or 4)")
return secondPathChoice
def InsideCastlePath(secondPathChoice):
print("...................................................................")
print("Levi is put into a jail cell.")
time.sleep(2)
print("He notices some loose wall shards laying on the ground.")
time.sleep(2)
print("Levi picks up the shards and wonders if he can sharpen one of them to make a lockpick.")
time.sleep(2)
print("Through the night he works tirelessly to make a lockpick, and by morning he successfully makes one.")
time.sleep(2)
print("Levi must escape the jail cell when the guards are not paying attention.")
successfulLockpick = random.randint(3,4)
if secondPathChoice == str(successfulLockpick):
print(".................................................................")
print("Levi successfully lockpicks the jail cell door and makes his way to the kings treasure room.")
time.sleep(2)
print("")
else:
print("..................................................................")
print("The lockpick breaks off in the jail cell door and Levi never escapes.")
Restart="yes"
while Restart == "yes" or Restart =="y":
intro()
choice = pathChoice()
checkPath(choice)
castleScene()
choice2 = secondPathChoice()
InsideCastlePath(choice2)
Restart=input("Would you like to Restart Levi's Adventure? (yes or y to `restart): ")
答案 0 :(得分:2)
您有错字:
def secondPathChoice(): pat="" while pat !="3" and pat !="4": path = input("What does Levi do? (3 or 4)") return secondPathChoice
是pat
而不是path
=> 无限循环
您应该在DRY: don't repeat yourself
之后稍微捆绑一下功能def get_input(text, options):
pat = ""
while pat not in options:
pat = input(text)
return pat
并像这样使用它:
answer1 = get_input("What will Levi do? (1 or 2): ", {"1","2"})
answer2 = get_input("What does Levi do? (3 or 4): ", {"3","4"})
这样,您可以重复使用要求用户输入直到使用不同的文本和选项才有效的逻辑,而无需再次重新编码该方法。
另请参阅:Asking the user for input until they give a valid response
还有一些小技巧:
while pat !="3" and pat !="4":
更简洁地写为
while pat not in {"3","4"}:
如果您需要打印相同字符的倍数,请使用print("." * 66)
打印66个点。
我不太明白为什么要使用随机化-玩家选择一个选项,然后改变正确的选择。这很有用,因此他们无法记住“正确”的解决方案..但是……与您从不询问用户而只是选择一个随机路径一样……
答案 1 :(得分:1)
欢迎来到神话般的编程世界;)
与现实生活不同,这里的拼写非常重要。单个字符可以构成或破坏您的程序,如此处所示:
def secondPathChoice():
pat=""
while pat !="3" and pat !="4":
path=input("What does Levi do? (3 or 4)")
return secondPathChoice
pat
不是path
,也不是secondPathChoice
。
祝你好运!