Here is an example of a Python procedure that could be useful in a school setting:

Copy code
def calculate_average_grade(grades):
    """
    This procedure calculates the average grade for a given list of grades.
    It takes in a list of grades as input and returns the average grade as output.
    """
    total_grade = 0
    for grade in grades:
        total_grade += grade
    average_grade = total_grade / len(grades)
    return average_grade

# Example usage:
grades = [90, 80, 70, 60]
average = calculate_average_grade(grades)
print(average) # Output: 75.0

To call this procedure, we would simply need to use the syntax calculate_average_grade(grades), where grades is a list of grades that we want to calculate the average for. This procedure could be useful for a student or a teacher to quickly and easily calculate the average grade for a given set of grades.

def calculate_letter_grade(grade):
    """
    This procedure calculates the letter grade for a given numeric grade.
    It takes in a numeric grade as input and returns the corresponding letter grade as output.
    """
    if grade >= 90:
        return "A"
    elif grade >= 80:
        return "B"
    elif grade >= 70:
        return "C"
    elif grade >= 60:
        return "D"
    else:
        return "F"

# Example usage:
numeric_grade = 87
letter_grade = calculate_letter_grade(numeric_grade)
print(letter_grade) # Output: "B"

To call this procedure, we would simply need to use the syntax calculate_letter_grade(numeric_grade), where numeric_grade is a numeric grade that we want to convert to a letter grade. This procedure could be useful for a student or a teacher to quickly and easily convert a numeric grade to a letter grade.

Notes Calling and Developing Procedures

Calling Procedures Slide 1:

  • A procedure is a named group of programming instructions that may have parameters and return values. Procedures are referred to by different names, such as method or functions, depending on the programing language. Parameters are input values of a procedure. Arguments specify the values of the parameters when procedure is called.
  • A procedure call interrupts the sequential execution of statements causing the program to execute the statements within the procedure before continuing. One the last statement in the procedure (or a return statement) has executed, flow or control is returned to the point immediately following where the procedure was called. Slide 2:
  • When calling procedures, it's important to take notice to whether it returns data, or a block of statements.
  • If the procedure just returns a block of statements, you call the procedure by referring to the procedure name, and inputting the arguments.
  • If the procedure returns some sort of data like a boolean or value, then you will assign that value to a variable Slide 3:
  • Assume the Temperature outside is Fahrenheit.
  • The procedure convertFahrenheit is intended to convert from Fahrenheit to Celsius.
  • Convert the following psuedocode to python
def convertFahrenheit(temperature):
    celsius = temperature - 32
    celsius = celsius * 5/9
    return celsius

outsideTemp = input("What is the temperature outside?")
outsideTemp = (convertFahrenheit(int(outsideTemp)))
print(outsideTemp)
32.22222222222222

Developing Procedures Slide 8: Picking a descriptive name is important in case you revisit the code later on (separate words with capitals) There are 2 different types of procedures- ones that return a value and those that simply execute a block of statements Steps of developing procedure: picking a useful name, thinking of parameters (what data does the procedure need to know), making a flowchart or writing procedure in pseudocode, and actually developing the procedure.

Slide 9: In this example, a teacher is writing a program that will replace the grade on a previous quiz if the new grade is better than the previous.

  • What would be a good name for this procedure?
  • What parameters do we need for this procedure?
  • Try writing this procedure out in python based on the given pseudocode
quizGrade = 32
def currentGrade(currentPoints):    
    currentGrade = currentPoints / 44
    currentGrade = currentGrade * 100
    return currentGrade

newPoints = int(input("how many points do you currently have?"))
newPercent = (currentGrade(int(newPoints)))

if (newPoints > quizGrade):
    newquizGrade = newPercent
    print("your new grade is: " + str(newquizGrade))
else:
    print("your score is still " + str(quizGrade))
your new grade is: 152.27272727272728

Procedural Abstraction

  • One type of abstraction is __ abstraction which provides a name for a process and allows a procedure to be used only knowing what it does and not how it does it
  • This is very helpful in managing __ in a program
  • Subdivision of a program into separate subprograms is called __
  • A procedural abstraction may __ shared features to generalize functionality instead of duplicating code. This allows for program reuse, which helps manage complexity
  • When a pre-written procedure is called, you don’t necessarily need to know the details of this, just what it does and how to call it
  • Simply, procedural abstraction is naming and calling a __ procedure
  • Making sure to include the right arguments so the __ can do what its supposed to do is crucial Complexity Example One of the biggest advantages of procedural abstraction is managing complexity.

Think about the process of simplifying the code? What do you think the advantage of the code segment on the left is? MOVE_FORWARD()|turnCorner()| ROTATE_RIGHT |MOVE_FORWARD()| MOVE_FORWARD()|MOVE_FORWARD()| MOVE_FORWARD() ROTATE_RIGHT() MOVE_FORWARD() ROTATE_LEFT() MOVE_FORWARD() ROTATE_LEFT() MOVE_FORWARD() MOVE_FORWARD MOVE_FORWARD()