print(chr(0b00100011))*a. # b. $ c. % d. *
def f(n): return n * nthe identifier n is a(n) ________ .
fin = open("input.txt", "r")a. instance method *b. standalone method
fin.close( )*a. instance method b. standalone method
def get_greeting(the_name): return "Hello, " + the_name + ", how are you?" print(get_greeting("Larry"))This is a traditional test of the get_greeting method.
# ----- Source code file: greeting.py def get_greeting(the_name): return "Hello, " + the_name + ", how are you?" # ----- Source code file: test1.py from greeting import get_greeting print(get_greeting('Larry'))
# Method 1 from modulename import methodnameand
# Method 2 import modulenameAns: with Method 1, the method does not need to be fully qualified. with Method 2, the method must be fully qualified by prefixing it with the module name. With Method 2, the test2.py module looks like this:
import greeting print(greeting.get_greeting('Larry'))
assertEqual(max(37, 71), 71) assertEqual(max(11, 15), 15)For each assert statement that is true, the unit test passes; for each assert statement that is false, the unit test fails.
import unittest class TestStringMethods(unittest.TestCase): def test_2(self): assertEqual(max(37, 71), 71) assertEqual(max(11, 15), 15) if __name__ == '__main__': unittest.main( ) # Output: . ----------------------------------------------------- Ran 1 test in 0.002s OKThe conclusion is OK, so the test succeeded.
name = "Larry" amt = 235.41 print(name + 'owes me $' + amt + ".") # Ans: name = "Larry" amt = 235.41 # Method 2, concatenation. fout.write only accepts one argument. fout.write(name + " owes me $" + str(amt) + ".\n") # Method 3, f-string. fout.write(f"{name} owes me {amt}.\n") fout.close( )
# Ans: # Input file input.txt contents: Larry,235.41 # Debugging print statements are # commented out. fin = open("input.txt", "r") # print(type(fin)) line = fin.readline( ).strip( ) # print(line) fin.close( ) fields = line.split(",") # print(fields) name = fields[0] amt = float(fields[1]) # print(name, amt) # print(f'{name} owes me ${amt}.') fout = open("output.txt", "w") fout.write(f'{name} owes me ${amt}.\n') fout.close( )
1 input_string = input("Enter a Fahrenheit temperature: ") 2 cel = int(input_string) 3 fahr = 9 * cel // 5 + 32 4 print("Fahrenheit temperature:", fahr)Extract the method and create a hardcoded test script: convert_cel_to_fahr from lines 2 and 3 of the preceding script. For these examples, a refactored method does not include any input or print statements. Also write a unit test to test the extracted method:
// ---- Module: convert.py def convert_cel_to_fahr(input_string): cel = int(input_string) fahr = round(9 * cel / 5 + 32) return fahr // ---- Module: test1.py print(convert_cel_to_fahr(0)) print(convert_cel_to_fahr(100)) print(convert_cel_to_fahr(20)) // ---- Module: test2.py import convert import unittest class MyUnitTestClass(unittest.TestCase): def test_1(self): self.assertEqual(convert_cel_to_fahr(0), 32) self.assertEqual(convert_cel_to_fahr(100), 212) self.assertEqual(convert_cel_to_fahr(20), 68) if __name__ == '__main__': unittest.main( )
["Frame0", [7, 2], [8, 2], [10], [10], [10], [10], [10], [9, 1], [10], [10], [7, 3]]"Frame0" is prepended to the array as a placeholder for Frame 0, which is not actually a frame in a bowling game. This is so the index of the first frame [7, 2] is 1. What is the score for each frame? Ans:
Frame Number | Frame as Array | Frame Score |
---|---|---|
1 | [7, 2] | 9 |
2 | [8, 2] | 8 + 2 + 10 = 20 |
3 | [10] | 10 + 10 + 10 = 30 |
4 | [10] | 10 + 10 + 10 = 30 |
5 | [10] | 10 + 10 + 10 = 30 |
6 | [10] | 10 + 10 + 9 = 29 |
7 | [10] | 10 + 9 + 1 = 20 |
8 | [9, 1] | 9 + 1 + 10 = 20 |
9 | [10] | 10 + 10 + 7 = 27 |
10 | [10] | 10 + 7 + 3 = 20 |
Bonus | [7, 3] |