#---------------------------------------------------------------------- # Program to print out student performance. # Final Version # # Copyright (C) February 2, 2018 -- Dr. William T. Verts # # There are three major changes in this version: # 1: Rather than explicitly print S inside the function, we return # S to the calling program, where it can decide what to do with # the string (save it, print it, etc.) # 2: Because there is only a SINGLE statement after each of the # if, elif, or else statements, those statements can be brought # up to the same line. (This will not be allowed if there were # more than one statement in each block.) # 3: Because the bar-graph is the first item in the string, we can # use the length of the string instead of an explicit counter # variable. (By getting rid of the counter, there is now only # one statement in the while-loop, so that statement can be # brought up to the same line as the while itself.) # Finally, we move the comments to the side and pack out the # empty lines. Function Student is now about as small as it can get. #---------------------------------------------------------------------- def Student (Name,Grade): S = "" while (len(S) < Grade): S = S + "|" # BarGraph S = S + " " + Name + " " + str(Grade) # Name and Grade if (Grade >= 90): S = S + " A" # Letter Grade elif (Grade >= 80): S = S + " B" elif (Grade >= 70): S = S + " C" elif (Grade >= 60): S = S + " D" else: S = S + " F" return S print Student("Fred",76) print Student("Sam",23) print Student("Mary",92)