Python 初学者Q:如何使用if/else缩进中声明的变量
在这一点上,我已经尝试了我能想到的一切。我正在尝试创建我的第一个游戏,玩家可以通过输入一个单词的答案来做出选择 当问题被孤立时,游戏就开始了。但是,如果我想创建一个将玩家重定向到游戏另一部分的决策(第7行通过river1变量直接到第10行),我会收到错误:NameError:name'river1'未定义,即使我在第7行定义了它 我认为这是因为它是在缩进中定义的。然而,我不知道如何修复它,并且仍然可以按要求工作。任何帮助都将不胜感激。谢谢Python 初学者Q:如何使用if/else缩进中声明的变量,python,Python,在这一点上,我已经尝试了我能想到的一切。我正在尝试创建我的第一个游戏,玩家可以通过输入一个单词的答案来做出选择 当问题被孤立时,游戏就开始了。但是,如果我想创建一个将玩家重定向到游戏另一部分的决策(第7行通过river1变量直接到第10行),我会收到错误:NameError:name'river1'未定义,即使我在第7行定义了它 我认为这是因为它是在缩进中定义的。然而,我不知道如何修复它,并且仍然可以按要求工作。任何帮助都将不胜感激。谢谢 print("Welcome to Treas
print("Welcome to Treasure Island! \nIts your mission to find the treasure.")
crossroads = input("You begin at a cross roads, do you go left or right? ").lower()
if crossroads == "left":
enemies = input("You ran into enemies, attack? ").lower()
if enemies == "no":
print("You ran to the lake")
river1 = input("You found a long river, it leads somewhere. Swim or wait? ").lower()
else:
print("You were overpowered and died, game over")
if crossroads == "right":
river1
if river1 == "swim":
print("You swam and drowned, Game over")
else:
print("You found a boat")
对于只能在特定情况下定义的变量,您有两个选项。首先,您可以提前申报:
print("Welcome to Treasure Island! \nIts your mission to find the treasure.")
crossroads = input("You begin at a cross roads, do you go left or right? ").lower()
enemies = None
river1 = None
if crossroads == "left":
...
或者,您可以测试它们是否存在:
if crossroads == "right":
if river1 and river1 == "swim":
...
解释程序的工作流程。我尝试了你的程序&得到了这个
C:\Users\LENOVO\Desktop\so>python try.py
Welcome to Treasure Island!
Its your mission to find the treasure.
You begin at a cross roads, do you go left or right? left
You ran into enemies, attack? no
You ran to the lake
You found a long river, it leads somewhere. Swim or wait? right
我相信您正在寻找的是如何重用代码。 使用函数
def river1_func():
choice = input("You found a long river, it leads somewhere. Swim or wait? ").lower()
if choice == "swim":
print("You swam and drowned, Game over")
else:
print("You found a boat")
if __name__ == "__main__":
print("Welcome to Treasure Island! \nIts your mission to find the treasure.")
crossroads = input("You begin at a cross roads, do you go left or right? ").lower()
if crossroads == "left":
enemies = input("You ran into enemies, attack? ").lower()
if enemies == "no":
print("You ran to the lake")
river1_func()
else:
print("You were overpowered and died, game over")
if crossroads == "right":
river1_func()
定义意味着将变量设置为一个值。如果没有执行这样的设置,则变量未定义。Ari,我认为程序逻辑中存在错误。若你们从上到下选择选项,那个么它是可以的,但若你们立即转到右边的纵横字谜,那个么这是程序第一次看到变量。您可以重新定义它或生成函数
river1 = ''
print("Welcome to Treasure Island! \nIts your mission to find the treasure.")
crossroads = input("You begin at a cross roads, do you go left or right? ").lower()
if crossroads == "left":
enemies = input("You ran into enemies, attack? ").lower()
if enemies == "no":
print("You ran to the lake")
river1 = input("You found a long river, it leads somewhere. Swim or wait? ").lower()
else:
print("You were overpowered and died, game over")
if crossroads == "right":
river1
if river1 == "swim":
print("You swam and drowned, Game over")
else:
print("You found a boat")
def river1_func():
choice = input("You found a long river, it leads somewhere. Swim or wait? ").lower()
if choice == "swim":
print("You swam and drowned, Game over")
else:
print("You found a boat")
if __name__ == "__main__":
print("Welcome to Treasure Island! \nIts your mission to find the treasure.")
crossroads = input("You begin at a cross roads, do you go left or right? ").lower()
if crossroads == "left":
enemies = input("You ran into enemies, attack? ").lower()
if enemies == "no":
print("You ran to the lake")
river1_func()
else:
print("You were overpowered and died, game over")
if crossroads == "right":
river1_func()