Python Forum
Need help with a homework problem on logical operators
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with a homework problem on logical operators
#1
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)))
Reply
#2
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)
Reply
#3
What's wrong with this code? I suspect you just need to remove comment symbols (#) to get it worked.
Reply
#4
So you go only if hungry, did not bring lunch, and coworkers going. Otherwise you stay and write code.
hungry and not brought_lunch....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with a formula 1 kind of homework claroque 2 1,396 Jun-20-2023, 09:58 PM
Last Post: claroque
  Bakery Problem.. Can you Help My Homework Cemvrs 1 2,347 Jan-14-2022, 05:47 PM
Last Post: Yoriz
  logical error for loop mcgrim 8 3,635 May-25-2019, 01:38 AM
Last Post: michalmonday
  Functions with conditionals and comparison operators Drone4four 9 12,903 Jan-01-2019, 06:48 PM
Last Post: Drone4four
  Guessing game with comparison operators Drone4four 9 13,730 Dec-02-2018, 06:12 PM
Last Post: ichabod801
  Homework Problem csascott 1 2,265 Oct-27-2018, 04:44 AM
Last Post: scidam
  Homework Problem Travisbulls34 1 2,935 Sep-11-2018, 04:04 PM
Last Post: ichabod801
  Help with homework problem - iterating a function midnitetots12 4 3,479 Feb-21-2018, 10:51 PM
Last Post: nilamo
  Problem with a homework Samuelcu 2 2,821 Feb-03-2018, 01:39 PM
Last Post: Samuelcu
  2 logical problems Peter_EU 1 2,955 Oct-26-2017, 04:13 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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