刚刚开始通过Zed的书“以艰难的方式学习python 3”来学习python。我目前正在从事基于文本的游戏练习。我运行了游戏,但无法弄清楚为什么此特定功能上的else语句不起作用。当我取出OR运算符并且仅在第一个if语句上等于一个选择时,它确实起作用。
def boss_floor():
boss_hp = 100
your_hp = 100
while boss_hp > 0:
print("Attack boss: 1.sword 2.arrow 3.magic")
attack = input('> ')
if attack == "sword" or "arrow" or "magic":
boss_hp -= random.choice(attack_value)
print("You attack the boss!")
print(f"Boss hp: {boss_hp}\n")
print("Big boss kicks you in the nuts")
your_hp -= random.choice(attack_value)
print(f"hp: {your_hp}\n")
else:
print("Input Invalid")
print(f"Boss hp: {boss_hp}")
if boss_hp <= 0:
print("You win!")
exit(0)
if your_hp <= 0:
print("You lose!")
exit(0)
答案 0 :(得分:0)
我认为您需要使用:
if attack == "sword" or attack == "arrow" or attack == "magic"
因为or "arrow"
不是条件,所以只是一个字符串。
答案 1 :(得分:0)
在您的import numpy as np
A = np.arange(1, 31)
B = A.reshape((3, 10))
print(B)
[[ 1 2 3 4 5 6 7 8 9 10]
[11 12 13 14 15 16 17 18 19 20]
[21 22 23 24 25 26 27 28 29 30]]
语句中,您需要针对每个if
子句与or
进行比较。所以,
attack