简体   繁体   中英

How do I use elif for multiple boolean options

I'm trying to use True and False in my if and elif to determine different outcomes. But for some reason when I try to get a elif response I always get a else response.

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?")

instead setting True value on matching both condition use those condition directly to get your answer. let say after getting input you should proceed this way:

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

such way you can directly use conditions to get as many possibilities you want to check.

the main problem with your logic is that you are setting both values to True only if single condition match otherwise their values will not change.

If you want to proceed for boolean logic you can also try below code :

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

You need to split out your if tAnswer == "yes" and currentMoney >= 85 into two statements, so they can be processed independently.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM