Python Forum

Full Version: Need help with a homework problem on logical operators
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm having a hard time getting this formula right.

My attempts are under the line of code that says "#Write your code here!" Any suggestions?


hungry = True
coworkers_going = False
brought_lunch = False

#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.

#Imagine you're deciding whether or not to go out to lunch.
#You only want to go if you're hungry. If you're hungry, even
#then you only want to go if you didn't bring lunch or if
#your coworkers are going. If your coworkers are going, you
#still want to go to be social even if you brought your lunch.
#
#Write some code that will use the three boolean variables
#defined above to print whether or not to go out to lunch.
#It should print True if hungry is True, and either
#coworkers_going is True or brought_lunch is False.
#Otherwise, it should print False.


#Write your code here!
#print((hungry and (brought_lunch or not brought_lunch or not coworkers_going)) and not coworkers_going)
#print((hungry or not coworkers_going) and (brought_lunch or not brought_lunch) and coworkers_going)
#print(hungry and (brought_lunch and (not coworkers_going or coworkers_going)))
#print(hungry and ((brought_lunch or not coworkers_going) or (not brought_lunch or coworkers_going)))
#print(hungry and (brought_lunch or not coworkers_going))

#if hungry:
#    if coworkers_going:
#        print(True)
#    elif not brought_lunch:
#        print(True)
#    else:
#        print(False)
#else:
#    print(False)

#print((hungry and (brought_lunch or not coworkers_going)) or (not brought_lunch and coworkers_going))
#print((hungry and (brought_lunch or not brought_lunch)) and not coworkers_going)
#print(hungry and ((brought_lunch or not brought_lunch) and (coworkers_going or not coworkers_going)))
#print(hungry and ((brought_lunch and not coworkers_going) or (not brought_lunch and not coworkers_going)))
The if statements for checking whether or not to go to lunch can be rearranged in this way
hungry = False
coworkers_going = False
brought_lunch = True

if brought_lunch and not coworkers_going:
    print(False)
elif coworkers_going and hungry:
    print(True)
elif not brought_lunch and hungry:
    print(True)
else:
    print(False)
What's wrong with this code? I suspect you just need to remove comment symbols (#) to get it worked.
So you go only if hungry, did not bring lunch, and coworkers going. Otherwise you stay and write code.
hungry and not brought_lunch....