如何将elif用于多个布尔选项

时间:2017-12-03 06:02:29

标签: 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?")

2 个答案:

答案 0 :(得分:1)

而是在匹配两个条件时设置True值直接使用这些条件来获得答案。 在收到输入之后,你应该这样说:

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

答案 1 :(得分:1)

您需要将if tAnswer == "yes" and currentMoney >= 85拆分为两个语句,以便可以单独处理。

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