如何对多个布尔选项使用elif

2024-10-01 17:23:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在ifelif中使用True和False来确定不同的结果。但由于某种原因,当我试图得到elif反应时,我总是得到else反应

money = False
answer = False
currentMoney = int(input("How much money do you have?\n"))
tAnswer = input("Did Timyzia say yes we can go?\n") 

if tAnswer == "yes" and currentMoney >= 85:
    money = True
    answer = True 

if answer == True and money == True:
    print("You can go on a date with Timyzia!")
elif answer == True and money == False:
    print("You need to get some more money Timyzia aint cheap.")
elif answer == False and money == True:
    print("Timyzia has to say yes for you to go out on a date, stupid!")
else:
    print("How you suppose to go on a date without permsssion and hvae no money?")

Tags: andtoansweryoufalsetruegodate
2条回答

相反,在匹配两个条件时设置真值,直接使用这些条件得到答案。 假设在得到输入后,您应该这样做:

if tAnswer. lower() == 'yes' and money < 85:
    print('you need more money')
elif tAnswer. lower() =='yes' and money >=85:
   print('get ready to go')

这样,您可以直接使用条件来获得您想要检查的任意多个可能性

逻辑的主要问题是,只有在单个条件匹配时才将两个值都设置为True,否则它们的值不会更改

如果要继续布尔逻辑,还可以尝试以下代码:

money = True if int(input('enter money:')) > 85 else False
answer = True if input('yes or no?').lower()=='yes' else False
# follow your logic of if.. elif condition

您需要将if tAnswer == "yes" and currentMoney >= 85拆分为两个语句,这样它们就可以独立处理了

if tAnswer == "yes":
    answer = True
if currentMoney >= 85:
    money = True

相关问题 更多 >

    热门问题