''' Purpose: gain experience with if-elif-else ''' # report message based on day of week # Sunday: weekend day # Monday: start of school week # Tuesday: school day # Wednesday: school day # Thursday: school day # Friday: end of school week # Saturday: weekend day # get day of interest reply = input( 'Enter day of week: ' ) # put day in standard format - first letter capitalized, others lower case day = reply.strip() day = day.capitalize() # day = reply.strip().capitalize() # alternatively could also be written as print( ) # analyze and report on day # to evaluate multiple expressions, we can use if/elif/else structures # if ( logical expression ): # elif ( logical expression 2 ): # you can have multiple elif statements # else: # optional # python will go down each if/elif statements ONE AT A TIME and will evaluate THE FIRST TRUE STATEMENT # and then do the action within that True if/elif/else statement # it does not matter if any of the statements after the first truth is True or not!! Just do the first thing # that's true # strategy 1: # is it Monday, is it Tuesday, .... if ( day == 'Sunday' ): print( 'weekend day' ) elif ( day == 'Monday' ): print( 'start of school week' ) elif ( day == 'Tuesday' ): print( 'school day' ) elif ( day == 'Wednesday' ): print( 'school day' ) elif ( day == 'Thursday' ): print( 'school day' ) elif ( day == 'Friday' ): print( 'end of school week' ) elif ( day == 'Saturday' ): print( 'weekend day' ) else: # if day is not any of the English day of the week print( 'bad input' ) # we could have included Saturday in the else statement as well # (no separate elif for checking whether day == 'Saturday') # but we may get bad inputs # which shouldn't print 'weekend day' when we get a bad input # strategy 2 --> shorter if ( ( day == 'Sunday' ) or ( day == 'Saturday' ) ): # if the day is a Sunday OR if day is a Saturday # only need one of these things need to be True: either day is a Sunday, or day is a Saturday # then this statement is True --> we want the same action to be done for either print( 'weekend day' ) # CANNOT DO day == 'Sunday' or 'Saturday' --> must do the statements SEPARATELY # still legal but not what you want # == is more important than or --> gets evaluated first before the or with the string 'Saturday' elif ( day == 'Monday' ): print( 'start of school week' ) elif ( day == 'Friday' ): print( 'end of school week' ) # other combo: 'Tuesday', 'Wednesday', 'Thursday' elif ( day in [ 'Tuesday', 'Wednesday', 'Thursday' ] ): # check if day is in the list which has the strings: 'Tuesday', 'Wednesday', 'Thursday' # if day is a string in this list, then print 'school day' --> same action for any of these strings print( 'school day' ) else: print( 'bad input' ) # if you have multiple if statements # python will check all if statements and if they're true, then do that action; # each if statement does not get ignore and each will get evaluated # Ex: # if ( something is true 1 ): # # do something # if ( something is true 2 ): # # do something # if you want to do both # do something if BOTH conditions are met, the above pseudo code will do that # for if/elif structures # python checks one statement at a time, if an if/elif/else statement is True --> do the action inside it # and IGNORES all OTHER elif/else statements # Ex: # if ( something is true 1 ): # # do something 1 # elif ( something is true 2 ): # # do something 2 # if something is true 1 evaluates to True --> then only do something 1 # WILL NOT do something 2 # doesn't matter if something is true 2 evaluates to True or False