Normality
Normality

Reputation: 33

Python- Boolean Cross-checking

I have an assignment on using ONLY boolean statements for this question: "Can I register today? A student can register on Monday if Senior, on Tuesday if Junior, on Wednesday if Sophomore, and on Thursday if Freshman."

Is there a simple way to cross check each and every day and status to see if they are either True or False without using if/else statements? If so, how would the code look?

Upvotes: 0

Views: 149

Answers (2)

reticentroot
reticentroot

Reputation: 3682

Perhaps something like this??

from datetime import datetime

def options():
  print """
Are you a? [Enter 0-4]
0) Senior
1) Junior
2) Sophomore
3) Freshman
4) quit
"""


print "Can I register today?"
while True:
  options()
  selection = raw_input("Enter status: ")
  try:
    selection = int(selection)
  except ValueError:
    print "Invalid entry please try again"
    continue
  # if you can't use any if statements
  # replace the if statement with a print statement saying to exit using control c
  # or above the while loop, create the variable selection = None
  # change the while loop from a while True to while selection != 4:
  # I leave it up to you :-)
  if selection == 4:
    break
  # 0 is monday
  weekday = datetime.today().weekday()
  print
  print "reply: {0}".format(selection == weekday)

Upvotes: 0

Anil_M
Anil_M

Reputation: 11473

Here is a solution using a Python dictionary and Boolean comparison.

We use a dictionary to create student status to day of week associations. Then get user input and compare using a Boolean expression.

from datetime import date
import calendar

my_dict = {'Senior' : 'Monday',
           'Junior' : 'Tuesday',
           'Sophomore' : 'Wednesday',
           'Freshman' : 'Thursday'
           }


my_date = date.today()

my_status = (raw_input("Enter your enrollment status: ")) #Student current status

today = calendar.day_name[my_date.weekday()]  #Day or week

reg_status = today == my_dict[my_status]   

#who can register today
who_can_register = my_dict.keys()[my_dict.values().index(today)] 

print "Can you register=", reg_status 
print "Today is  ", today , " Only ", who_can_register, "can register"

Output:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Enter your enrollment status: Senior
Can you register= False
Today is   Thursday  Only  Freshman can register
>>> ================================ RESTART ================================
>>> 
Enter your enrollment status: Junior
Can you register= False
Today is   Thursday  Only  Freshman can register
>>> ================================ RESTART ================================
>>> 
Enter your enrollment status: Sophomore
Can you register= False
Today is   Thursday  Only  Freshman can register
>>> ================================ RESTART ================================
>>> 
Enter your enrollment status: Freshman
Can you register= True
Today is   Thursday  Only  Freshman can register
>>>

Upvotes: 1

Related Questions