gpt4 book ai didi

python - Python 中错误的 if-else 情况

转载 作者:行者123 更新时间:2023-11-30 23:08:08 25 4
gpt4 key购买 nike

有人可以看看这个并告诉我出了什么问题吗?我尝试运行它,但是当你说 no 时下雨和 no对于下雪,它仍然打印出 if true 语句 You can't ride your bike

raining = input("Is it raining right now?\nYes or No?\n")
if raining.lower() == "yes" :
raining = True
else:
raining = False
snowing = input("Is it snowing out?\nYes or No\n")
if snowing.lower() == "yes" :
snowing = True
else:
snowing = False
if raining or snowing == True :
print("You can't ride your bike")
if raining and snowing == False :
print("You can ride your bike")

cost = input("How much is your new PC?")
cash = input("How much money do you have?")
total = float(cash) + float(cost)
if total < 0 :
print("You can't buy it")
if total >= 0 :
print ("You can buy it")

最佳答案

if raining and Snowing == False 解释为:

if raining == True and snowing == False

您应该按如下方式更新第二个 if 语句:

if raining == False and snowing == False:
...

由于 if rainingif raining == True 在检查 bool 值方面是相同的,因此您可以像这样简化逻辑:

if raining or snowing:
print("You can't ride your bike")
else:
# implies "not (raining or snowing)" or "(not raining) and (not snowing)"
print("You can ride your bike")

关于python - Python 中错误的 if-else 情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31901626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com