Python Forum
How do you create a true/false response?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you create a true/false response?
#1
Hello,

I am trying to create an accurate output (below snapshot is not giving me what I need) when I trigger the user to type in the meals they had 24 hours ago like: meat, chocolate, dairy. The output should say False for meat and everything else true but I am not getting that - can someone help me?

Input
menu = '"seafood, dairy, nuts, chocolate"'
input_test = input("enter food categories eaten in the last 24 hours: ").lower()
print('It is',"dairy" in input_test.lower(),"that",menu,'contains',"dairy")
print('It is',"nuts" in input_test.lower(),"that",menu,'contains',"nuts")
print('It is',"seafood" in input_test.lower(),"that",menu,'contains',"seafood")
print('It is',"chocolate" in input_test.lower(),"that",menu,'contains',"chocolate")

Output
enter food categories eaten in the last 24 hours: meat, seafood, nutS
It is False that "seafood, dairy, nuts, chocolate" contains dairy
It is True that "seafood, dairy, nuts, chocolate" contains nuts
It is True that "seafood, dairy, nuts, chocolate" contains seafood
It is False that "seafood, dairy, nuts, chocolate" contains chocolate
Reply
#2
for the love of God use the format method. I would hope that schools are not teaching to write code like that.
>>> 'It is {} that {} contains dairy'.format('dairy' in input_test.lower(), menu)
'It is False that "seafood, dairy, nuts, chocolate" contains dairy'
much easier to distinguish the difference of string and variable. Or use the python3.6+ f-string
>>> is_dairy = 'dairy' in input_test.lower()
>>> f'It is {is_dairy} that {menu} contains dairy'
'It is False that "seafood, dairy, nuts, chocolate" contains dairy'
it would totally depend on what you enter at your input as to whether its true or false. If your trying to get the menu a it might look in your actual string you could just do
>>> 'dairy' in menu
True
Recommended Tutorials:
Reply
#3
Your program works as expected but the logic is wrong. You compare a list of ingredients with the variable input_text but you display a result that gives the impression you compare a list of ingredients with the variable menu.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HELP - Writing code returning True or False Kokuzuma 2 2,762 Nov-01-2018, 03:37 AM
Last Post: Kokuzuma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020