#---------------------------------------------------------------------- # February 13, 2019 #---------------------------------------------------------------------- #********************************************************************** # Task: Write a function that accepts a student score as a parameter # and prints the corresponding grade (print an "A" for scores >= 90, # print a "B" for scores >= 80, etc.) #********************************************************************** #---------------------------------------------------------------------- # Solution #1: Use standard if-else structures. Notice the indentation # of every level. Indentation increases with every if-else structure. #---------------------------------------------------------------------- def PrintGrade1 (Grade): if Grade >= 90: print ("A") else: if Grade >= 80: print ("B") else: if Grade >= 70: print ("C") else: if Grade >= 60: print ("D") else: print ("F") return #---------------------------------------------------------------------- # Solution #2: This version has all five if statements in order, but # each is independent of all others, so each test has to be very # specific to not print more than one answer. However, even if we # find that a student will get an "A", we **still** have to check to # see if they get a "B", a "C", a "D", or an "F". This is very # inefficient. While it works correctly, it is badly designed code. #---------------------------------------------------------------------- def PrintGrade2 (Grade): if (Grade >= 90): print ("A") if (Grade >= 80) and (Grade < 90): print ("B") if (Grade >= 70) and (Grade < 80): print ("C") if (Grade >= 60) and (Grade < 70): print ("D") if (Grade >= 0) and (Grade < 60): print ("F") return #---------------------------------------------------------------------- # Solution #3: This version is simpler than both earlier versions. # Each elif structure replaces an else followed by an if. Since # the if-elif-elif-elif-else are all part of the same structure, # they all have the same indentation level. # # You cannot start with an elif or an else; you must start with an if, # follow that by any needed elif statements, and end with the else # (assuming that an else is needed). #---------------------------------------------------------------------- def PrintGrade3 (Grade): if Grade >= 90: print ("A") elif Grade >= 80: print ("B") elif Grade >= 70: print ("C") elif Grade >= 60: print ("D") else: print ("F") return #---------------------------------------------------------------------- # Solution #4: There is a rule that any indented block as part of an # if, elif, else, or while contains ONLY ONE statement, then that # single statement may be placed on the same line after the colon. # This is NOT possible if the indented block contains more than one # statement. #---------------------------------------------------------------------- def PrintGrade4 (Grade): if Grade >= 90: print ("A") elif Grade >= 80: print ("B") elif Grade >= 70: print ("C") elif Grade >= 60: print ("D") else: print ("F") return #---------------------------------------------------------------------- # Solution #5: This is exactly the same as solution #4, except that # extra spaces have been inserted to line up similar sections. While # not required by Python, doing this can help you find mistakes because # violations of the similarities between sections will be obvious. Use # your wonderful primate visual systems to identify patterns! #---------------------------------------------------------------------- def PrintGrade5 (Grade): if Grade >= 90: print ("A") elif Grade >= 80: print ("B") elif Grade >= 70: print ("C") elif Grade >= 60: print ("D") else: print ("F") return