Midterm Review Question Answers Part A: 1. a 2. b 3. d 4. c 5. c 6. a,d 7. b 8. b 9. a 10. c 11. b 12. b 13. a 14. a Part B: The order of the operators from highest priority on the left to lowest priority on the right is: ( ) [ ] . ** * + == and = Part C: Correct the errors See the error log below. Corrected Script: 1 # --- Source code file vendingmachine.py ------------------- 2 # This vending machine dispenses one type of 3 # candy bar, which is paid for with 3 quarters. 4 class VendingMachine: 5 def __init__(self): 6 self._num_candy_bars = 3 7 self._num_quarters = 0 8 def insert_quarter(self): 9 self._num_quarters += 1 10 return "Quarter inserted." 11 def dispense_candy_bar(self): 12 if self._num_quarters >= 3 and \ 13 self._num_candy_bars > 0: 14 self._num_quarters = 0 15 self._num_candy_bars -= 1 16 return "Candy bar dispensed." 17 else: 18 return "VM out of candy bars." 19 def __str__(self): 20 return f"{self._num_candy_bars} " + \ 21 f"{self._num_quarters}" 22 # --- Source code file: test1.py --------------------- 23 from vendingmachine import VendingMachine 24 vm = VendingMachine( ) 25 print(vm) 26 print( ) 27 print(vm.insert_quarter( )) 28 print(vm) 29 print(vm.insert_quarter( )) 30 print(vm.insert_quarter( )) 31 print(vm.dispense_candy_bar( )) 32 print(vm) 33 for i in range(0, 3): 34 print( ) 35 for j in range(0, 3): 36 print(vm.insert_quarter( )) 37 vm.dispense_candy_bar( ) 38 print(vm) Error Log: Line 4: Change vendingmachine to VendingMachine. Line 5: Add colon at the end of line. Line 6: Change == to =. Line 7: Add self. to beginning of line. Line 9: Remove space between + and = to make +=. Line 11: Change dashes to underscores and change ( ) to (self). Line 17: Add colon to end of line. Line 21: Add f to beginning of line. Line 24: Remove new from line. Line 33: Replace colon by comma, add colon to end of line. Line 34: Add ( ) to end of line. Line 36: Replace self by vm. Line 37: Add vm. to beginning of line. Part D: Predict the Output Output from test1.py Ford Silver 35 0 False Part D: Convert Traditional Test to Unit Test. To convert a traditional test script to a unit test script, just replace print statements by assertEqual statements. Leave the other statements as is. # test2.py script: from automobile import Automobile import unittest class MyUnitTestClass(unittest.TestCase): def test_1(self): # Sample assertEqual statement: # self.assertEqual(computed, expected) from automobile import Automobile auto = Automobile("Ford", "Silver") self.assertEqual(str(auto), "Ford Silver") auto.turn_key("on") auto.shift("Drive") auto.press_accelerator(20) auto.press_accelerator(15) self.assertEqual(auto.speed, 35) auto.press_brake(40) self.assertEqual(auto.speed, 0) auto.turn_key("off") self.assertEqual(auto.is_running, False) if __name__ == '__main__': unittest.main( ) # Output: . --------------------------------------------------------- Ran 1 test in 0.015s OK