【发布时间】:2020-12-31 17:23:55
【问题描述】:
我有一个名为 programRepeated 的布尔值,我需要使用 if / else 语句检查它是真还是假。有没有办法做到这一点?
if programRepeated = True:
print("Your balance is : $" , final_balance + cash_insert)
print("")
elif programRepeated = False:
print("Your balance is : $" , final_balance + cash_insert - 5)
print("")
【问题讨论】:
-
= 是赋值。请参阅教程,该教程还应涵盖 ==(比较)。另外,可以只使用
if x:,当 x 是真假时。 -
布尔值不需要比较。这就是布尔值的意义所在。只需使用
if programRepeated: -
除了
True或False之外,其他值是否应该被视为真值、假值或两者都不是?我会注意到这是真的基本的Python;我强烈建议通过a tutorial on the language as a whole 运行,因为通过 Stack Overflow 问题零碎学习基础知识是不切实际的。 -
这能回答你的问题吗? How do I use a Boolean in Python?
-
注意,对象默认被认为是 True,除非它们满足 False 的条件:stackoverflow.com/questions/63876672/…
标签: python