当我把3 if语句打印出所有这些时

时间:2015-05-15 21:56:01

标签: python

以下使用的变量我没有显示初始化,但你可以自己编造。它完全符合我的要求,但它打印的所有if if不仅仅是1个if。

m = input()
if m == 1:
    print(l)
    time.sleep(1)
else:
    print(cost + f + baws)
    boss_he = boss_he - 20
    print("%" + str(boss_he) + " boss health left")

if m == 2:
    time.sleep(1)
else:
    print(cost + baws)
    boss_he = boss_he - 25
    print('%' + str(boss_he) + ' boss health left')
if m == 3:
    time.sleep(1)
else:
    print(h)
    wiz_he = wiz_he + 15
    print('%' + str(wiz_he) + " of you health left")

每当我运行此代码时,它将执行其他所有情况,为什么?

如果其他人打印1,或1或3,如果我把那个数字,它将打印1 2和3而不是只打印1(如果我要求它打印1)。我的代码有问题吗?

2 个答案:

答案 0 :(得分:3)

您的input()永远不会评估您的if个任务状态,因为您正在input()作为str。因此,所有else被触发。而是投射intm = int(input())。此外,您的逻辑存在缺陷,因为如果m不是123,则会输入所有else语句。

答案 1 :(得分:2)

为什么不使用if,elif,elif等而不是多次使用?并且程序将您的输入确定为字符串,以使其像m = int(input())一样将输入转换为整数