停止变量播放的问题

时间:2015-08-14 03:03:22

标签: python

我的编码时间不长(不到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

很抱歉,如果格式不正确,我很安静,文字选项会让我感到困惑。

1 个答案:

答案 0 :(得分:1)

问题在于:

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")
相关问题