在 if 函数中设置限制?

问题描述 投票:0回答:2

在第 14 行,我想通知用户他们的账户透支了。我希望当他们提款比当前余额多 50 时出现。如果他们超过这个,提款将被拒绝。

bal = input("What is your account balance?:            ")
intbal = int(bal)

withdraw = input("How much would you like to withdraw?:     ")
intwith = int(withdraw)

max = intbal + 50

if intwith <= intbal:

    print()
    print("Withdrawal successful. Current balance:   ", intbal - intwith)

elif intwith >= intbal or max:

    print()
    print("Withdrawal successful, the account is overdrawn. Current balance:        ", intbal - intwith)

elif intwith > max:

    print()
    print("Withdrawal refused, insufficient funds. Current balance:", intbal)

else:

    print("Invalid input")

我尝试使用显然不起作用的“或”。有哪些解决方案?

python if-statement currency
2个回答
1
投票

在 Python 中你可以链比较.

将第 14 行更改为.

elif max >= intwith > intbal:

0
投票

or 语句结合了两个布尔值。您只是在说 or max,因为 max(您应该使用另一个名称,因为 max 是一个内置函数)不是 0,所以它被视为 true。所以你的陈述总是读作“x or True”。相反,你应该这样做:

elif intwith >= intbal and intwith <= max:
© www.soinside.com 2019 - 2024. All rights reserved.