3 回答
TA贡献1803条经验 获得超6个赞
尝试这个:
weapon_choice = input("You can choose between three weapons to defeat the beast!\nPress 1 for Axe, 2 for Crossbow, 3 for Sword.\n")
if weapon_choice=='1':
print("You chose the Axe of Might")
elif weapon_choice=='2':
print("You chose the Sacred Crossbow")
else:
print("You chose the Elven Sword")
笔记 :
weapon_choice不需要像字符串str格式那样转换成格式。input()
每当你这样做input(1)或input(2)它基本上提示用户提供另一个输入而不是检查条件。
输出 :
michael@arkistarvh:/$ python text_game.py
You can choose between three weapons to defeat the beast! Press 1 for Axe, 2 for Crossbow, 3 for Sword.
3
You chose the Elven Sword
TA贡献1856条经验 获得超11个赞
这不是输入在 Python 中的工作方式。
您正确地假设 input("some text") 将在第一行打印该文本(并将结果存储在变量武器选择中),那么您为什么认为 input(number) 会返回一个布尔值,告诉您是否输入的是那个数字?
相反,它所做的是再次打印数字并返回一个空字符串(因为您可能只是按下了 Enter 键),因此前两个 if 为 False,程序进入 else,打印“Invalid input selected”。
您第一次输入的结果将存储在武器选择中,因此您应该对该变量进行比较。
TA贡献1811条经验 获得超4个赞
您应该改为:
weapon_choice = input("You can choose between three weapons to defeat the beast! \n " + " Press 1 for Axe, 2 for Crossbow, 3 for Sword.")
if weapon_choice == '1':
print("You chose the Axe of Might")
elif weapon_choice == '2':
print("You chose the Sacred Crossbow")
elif weapon_choice == '3':
print("You chose the Elven Sword")
else:
print("Invalid input selected")
这样做的原因是input(..)导致字符串被解析,因此不需要str(..)在input(..). 此外,您应该有一个传递无效输入的条件,以便更清楚地通知用户错误的根本原因。
添加回答
举报
