我试图捕获函数中用户的输入并分配给函数之外的变量,该变量稍后可以在代码中引用:
def WepSelection():
weapon_options = ["1", "2", "3"]
y = ""
while y not in weapon_options:
print("""Welcome, traveler. Before you begin your journey, please choose
from one of the following three weapons:
1) Sword
2) Spear
3) Bow and Arrow
""".format(name))
y = input("Please pick a weapon by entering the corresponding number (1, 2 or 3): ")
if y == weapon_options[0]:
y = "sword"
print("You have select the", y.name)
return y
elif y == weapon_options[1]:
y = "spear"
print("You have select the", y.name)
return y
elif y == weapon_options[2]:
y = "bow_arrow"
print("You have select the", y.name)
return y
最终,我希望发生的是,用户选择中的“y”变量(“sword”,“spear”或“bow_arrow”字符串)可以分配给变量,例如“selected_weapon” “这可能在功能之外。
例如,如果用户在函数中选择1,则外部变量selected_weapon =“sword”。
非常感谢。