[英]Issues with stopping a variable playing
我的编码时间还不够长(少于3周),所以我想测试一下并尝试一款基于文本的游戏。 我可能做得不好,但是当遇到一个决定时,我试图在另一个决定中嵌套另一个决定。 但是,当我尝试执行决策1的选项B时,它将尝试并运行嵌套在决策1的选项A下的决策,这导致了问题。 我会告诉你我的意思:
kill1 = input("Attack old man? (y/n) ")
if kill1 == "y":
print("You strike the old man with all your force")
time.sleep(1)
print("Old Man staggers back, his right arm crippled")
else:
print("Thanks for the staff")
if kill1 == "y":
kill2 = input("Strike again or run away? (s/r) ")
if kill2 == "s":
print("You swing the staff into the mans ribs")
time.sleep(1)
print("The man falls to the ground, incapacitiated")
else:
print("You run away into the woods")
输出/错误:
Attack old man? (y/n) n
Thanks for the staff
Traceback (most recent call last):
File "/Users/alex/Desktop/game.py", line 28, in <module>
if kill2 == "s":
NameError: name 'kill2' is not defined
抱歉,如果格式不正确,我是新手,文本选项使我感到困惑。
问题出在这里:
if kill1 == "y":
kill2 = input("Strike again or run away? (s/r) ")
if kill2 == "s":
print("You swing the staff into the mans ribs")
如果kill1
不是"y"
,则永远不会为变量kill2
分配值。
看起来您应该缩进代码,以便if kill2 == "s"
测试位于if kill1 == "y"
块中,如下所示:
if kill1 == "y":
kill2 = input("Strike again or run away? (s/r) ")
if kill2 == "s":
print("You swing the staff into the mans ribs")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.