Embercode
✕
Functions and ScopeQuick quiz

Refactoring in Python

6 questions. Covers Refactoring a long Python script.

Questions 1 to 5 of 6

  1. 1. Put the steps for one refactoring step in the order you do them.

    Use the arrows to put the steps in order.

    1. 1.Save the output to a file
    2. 2.Compare the new output with the saved reference using diff
    3. 3.Run the script, so Python reads the whole file and shows any indentation or name errors
    4. 4.Make one small change, such as moving one block of lines into a function
  2. 2. Put these steps for pulling the marks loop out of the main part of the program into sum_of in the correct order.

    Use the arrows to put the steps in order.

    1. 1.In the main loop, replace the inner for loop with the call total = sum_of(scores[i])
    2. 2.Run the program and save its output
    3. 3.Compare the new output with the saved reference using diff
    4. 4.Write def sum_of(marks): with the loop, and return total
  3. 3. After the marks loop moves into sum_of, why does the row loop still need total? ``python total = sum_of(scores[i]) average = total / len(scores[i]) # ... later in the same loop ... class_total = class_total + total ``
  4. 4. What does this program print? ``python def letter_grade(average): if average >= 90: return "A" elif average >= 80: return "B" elif average >= 70: return "C" elif average >= 60: return "D" print(letter_grade(95), letter_grade(55)) ``
  5. 5. This function should return True when a grade is not F. What is wrong with it? ``python def is_passing(grade): return grade == "F" ``

3 question(s) left on this page.