''' 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() # get rid of leading / trailing whitespace day = day.capitalize() # all characters are lowercase except the first character (uppercase) # print( 'day:', day ) # day = reply.strip().capitalize() # alternatively could also be written as print( ) # analyze and report on day # To Python, 'a' is not the same as 'A'. # Check 7 Different Days # if and elif have conditions / else does not have a condition # if ( 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' ) # elif( day == 'Sunday' ): # print( 'weekend day' ) # else: # not a day # print( 'That\'s not a day' ) # Grouping Days of the Week if ( day in ['Saturday', 'Sunday'] ): print( 'weekend day' ) elif ( day in ['Tuesday', 'Wednesday', 'Thursday']): print( 'school day' ) elif ( day == 'Monday' ): print( 'start of school week' ) elif ( day == 'Friday' ): print( 'end of school week' ) else: print( 'Not a day!' ) # Commenting out multiple lines of code: # Highlight the code # Mac: Cmd / # Windows: Ctrl / # This is a gotcha! # if ( ( day == 'Sunday' ) or ( day == 'Saturday' ) ): # RIGHT # print( 'weekend day' ) # if ( ( day == 'Sunday') or ('Saturday') ): # WRONG # ^ This part is always True (it will always jump into the block) # Same as doing if ( (day == 'Saturday') or True ): # Well one of them is true! So we always jump into this block! # This is the same thing as doing # if ('Saturday'): # 'Saturday' will always be True! (just the string itself is True) # ... if ( 'Monday' ): # 'Monday' as a string is ALWAYS True so we always print True print( True ) else: # We will never reach this code! print( False ) # 0 is always False # 1 and any literal (strings, numbers, actual values) are always True by default! # Computer science basically boils down to 0s and 1s. # 0 is False # 1 is True # So basically when we evaluate these expressions, any literal value (like the string 'Monday') # will always be True. # If we have a test expression though (day == 'Monday') then this gets evaluated and then # jumps into the block if that test expression / condition is True