''' Purpose: define some functions related to UVA ''' def credits_to_go( n ) : ''' Returns number of credits needed for graduation, given n credits earned so far ''' # When we use functions, we will pass in our "input" as arguments # to fill in the parameter n. Take a look at rotunda.py and # see how we call credits_to_go()! CLAS_GRADUATION_REQUIREMENT = 120 # credits_remaining = CLAS_GRADUATION_REQUIREMENT - n # Evaluate credits_remaining based on whether n is positive, 0 <= n <= 120, or >120 # You could also directly return your return values instead of doing credits_remaining = return value # if/elif/else -> test three DISTINCT possibilities of n! if ( n < 0 ): credits_remaining = None elif ( n <= CLAS_GRADUATION_REQUIREMENT ): credits_remaining = CLAS_GRADUATION_REQUIREMENT - n else: credits_remaining = 0 return credits_remaining # The function hands back this value! # The testers usually have the input() to get the arguments we wanna # pass in to replace our parameters. def how_old_is_UVA() : # This function has no parameters. Functions can but don't have to have parameters! ''' Returns the number of years since UVA was founded. ''' YEAR_OF_FOUNDING = 1819 import datetime # standard Python module that provides information about # current day and time # we can import it and use functions/values from it! current_time = datetime.datetime.now() # gives the current time current_year = current_time.year # This is how we get the current year! # We don't need the user to supply it. # or pass it in as an argument to a parameter # if we had one. age = current_year - YEAR_OF_FOUNDING # ex. 2020 - 1819 return age # Same function but with a parameter where we now have a parameter. def how_old_is_UVA_for_year( year ): # This is an example where a user can provide # an argument (like 2020) to provide the year to parameter year! YEAR_OF_FOUNDING = 1819 age = year - YEAR_OF_FOUNDING return age def grade_as_percent( score, total ): ''' Returns the percentage of right with respect to total (right/total) ''' grade = score/total percentage = grade * 100 result = str(percentage) + "%" # You can't concatenate an int and a string return result # We return a string of the percent and %