Review

For each of these, copy the code to a blank program. Then fill out the top part so that the bottom part works. Do not modify the bottom part in any way. Do not indent it or put it in a function. You are writing functions (or classes) in the top part that will satisfy all the tests in the bottom part.

Question 1

If you need help for this question, go back and review returning values from functions.

test_01.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Write a function called "f" that will pass all the tests below:




# --- Tests. Do not change anything below this line. ---
# Write your code above the line to pass all the tests.

if f(30, 40) == 70:
    print("Test 1 passed")
else:
    print("Test 1 failed")

if f(0, 0) == 0:
    print("Test 2 passed")
else:
    print("Test 2 failed")

if f(-20, 10) == -10:
    print("Test 3 passed")
else:
    print("Test 3 failed")

if f(0.5, 0.5) == 1:
    print("Test 4 passed")
else:
    print("Test 4 failed")

Question 2

test_02.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Write a function called "f" that will pass all the tests below:




# --- Tests. Do not change anything below this line. ---
# Write your code above the line to pass all the tests.

if f(0, 0) == 0:
    print("Test 1 passed")
else:
    print("Test 1 failed")

if f(5, 0) == 0:
    print("Test 2 passed")
else:
    print("Test 2 failed")

if f(10, 10) == 100:
    print("Test 3 passed")
else:
    print("Test 3 failed")

if f(0.5, 0.5) == 0.25:
    print("Test 4 passed")
else:
    print("Test 4 failed")

Question 3

This involves both division and addition.

test_03.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Write a function called "f" that will pass all the tests below:




# --- Tests. Do not change anything below this line. ---
# Write your code above the line to pass all the tests.

if f(10, 10, 1) == 20:
    print("Test 1 passed")
else:
    print("Test 1 failed")

if f(15, 10, 1) == 25:
    print("Test 2 passed")
else:
    print("Test 2 failed")

if f(10, 10, 2) == 10:
    print("Test 3 passed")
else:
    print("Test 3 failed")

if f(10, 10, 10) == 2:
    print("Test 4 passed")
else:
    print("Test 4 failed")

if f(4, 6, 2) == 5:
    print("Test 5 passed")
else:
    print("Test 5 failed")

if f(150, 50, 50) == 4:
    print("Test 6 passed")
else:
    print("Test 6 failed")

Question 4

Make sure the next one works for other temperatures, not just the ones we test. The function you write will not print anything, just return the proper value.

test_04.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Write a function called how_is_the_temperature that returns a string based
# on the temperature passed into it.
# Try to get this to work based on temperature ranges, rather than exact
# temperatures.



# --- Tests. Do not change anything below this line. ---
# Write your code above the line to pass all the tests.

if how_is_the_temperature(95) == "Hot":
    print("Test 1 passed")
else:
    print("Test 1 failed")

if how_is_the_temperature(85) == "Warm":
    print("Test 2 passed")
else:
    print("Test 2 failed")

if how_is_the_temperature(70) == "Great":
    print("Test 3 passed")
else:
    print("Test 3 failed")

if how_is_the_temperature(50) == "Cool":
    print("Test 4 passed")
else:
    print("Test 4 failed")

if how_is_the_temperature(30) == "Cold":
    print("Test 5 passed")
else:
    print("Test 5 failed")

Question 5

Make sure the next one works for other values, not just the ones we test. This question does not involve any printing, just returning.

test_05.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Write a function that figures out if a number is even or odd.
# Make it work for every number, not just the ones we test here.



# --- Tests. Do not change anything below this line. ---
# Write your code above the line to pass all the tests.

if even_or_odd(0) == "Even":
    print("Test 1 passed")
else:
    print("Test 1 failed")

if even_or_odd(1) == "Odd":
    print("Test 2 passed")
else:
    print("Test 2 failed")

if even_or_odd(-1) == "Odd":
    print("Test 3 passed")
else:
    print("Test 3 failed")

if even_or_odd(100) == "Even":
    print("Test 4 passed")
else:
    print("Test 4 failed")

if even_or_odd(1001) == "Odd":
    print("Test 5 passed")
else:
    print("Test 5 failed")

Question 6

If you need to, go back and review classes.

test_06.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Create a class called bird, with the attributes x and change_x, and a method
# called "move". Have it pass the tests below.



# --- Tests. Do not change anything below this line. ---
# Write your code above the line to pass all the tests.

my_bird = Bird()

# Test to see if an attribute was created correctly
if my_bird.x == 0:
    print("Test 1 passed")
else:
    print("Test 1 failed")

# Test to see if an attribute was created correctly
if my_bird.change_x == 0:
    print("Test 2 passed")
else:
    print("Test 2 failed")

# Test the move method to see if it properly moves the bird
my_bird.change_x = 5
my_bird.move()
my_bird.move()

if my_bird.x == 10:
    print("Test 3 passed")
else:
    print("Test 3 failed")