x = 5
y = 3
x = x + y
print(x)
print(y)
8
3
# What value is returned by this function call?
def double(number):
return number * 2
double(6)
12
# What is printed?
colors = ["red", "blue", "green"]
print(colors[1])
blue
# This code should print 10. What needs to be fixed?
x = 5
y = 5
print(x + Y)
Change Y to y
Which type of loop is the more natural choice: for or while?
Print every item in a list of student names.
for loop, we want to do something once for each item in a list
temperature = 72
if temperature >= 80:
print("hot")
elif temperature >= 60:
print("comfortable")
else:
print("cold")
print("done")
comfortable
done
# What are the parameter and argument in this code?
def greet(name):
print("Hello " + name)
greet("Taylor")
Parameter: name
Argument: "Taylor"
# What does the list contain after this code runs?
numbers = [2, 4, 6]
numbers.append(8)
numbers[0] = 10
[10, 4, 6, 8]
# This code should print "Adult" when age is 18 or older.
# What needs to be fixed?
age = 20
if age >= 18
print("Adult")
Add a colon after age >=18
Which type of loop is the more natural choice: for or while?
Keep asking the user to enter a number until they enter 0.
while loop
numbers = [4, 7, 2, 9]
total = 0
for number in numbers:
if number > 5:
total = total + number
print(total)
16
# What is printed?
def add_bonus(score):
score = score + 5
return score
grade = 82
new_grade = add_bonus(grade)
print(new_grade)
87
# What is printed?
numbers = [3, 6, 2, 8]
for number in numbers:
if number > 4:
print(number)
6
8
# This code should print every name in the list.
# What needs to be fixed?
names = ["Maya", "Leo", "Sam"]
for name in names:
print(name)
print(name) needs to be indented inside the for loop.
# Write one line of code that changes "cat" to "dog".
animals = ["fish", "cat", "bird"]
animals[1] = "dog"
def change(number):
number = number + 3
return number * 2
x = 4
y = change(x)
print(x)
print(y)
4
14
# This function should calculate a $5 discount and allow
# the result to be stored in a variable. What should be changed?
def discount(price):
new_price = price - 5
print(new_price)
final_price = discount(20)
Change print(new_price) to return new_price.
# What is the final value of total?
numbers = [2, 5, 3, 7]
total = 0
for number in numbers:
total = total + number
17
# This code should calculate the sum of the numbers.
# What needs to be fixed?
numbers = [4, 6, 3]
total = 0
for number in numbers:
total = number
print(total)
Change total = number to total = total + number.
The correct final value of total is 13.
# This function has three possible results: 0, 10, or 15.
# Choose THREE useful values of age to test the boundaries.
def ticket_price(age):
if age < 5:
return 0
elif age < 18:
return 10
else:
return 15
A particularly useful set is 4, 5, 18 or 5, 17, 18, because these test values immediately around the boundaries at 5 and 18.
def count_matches(values, limit):
count = 0
for value in values:
if value >= limit:
count = count + 1
return count
numbers = [4, 8, 3, 10, 8]
result = count_matches(numbers, 8)
if result > 2:
print("many")
else:
print("few")
print(result)
many
3
# Complete the function so it returns the larger of the two numbers.
def find_larger(a, b):
# YOUR CODE HERE
def find_larger(a, b):
if a > b:
return a
else:
return b
# What is printed? Explain what causes the loop to stop.
number = 1
total = 0
while total < 10:
total = total + number
number = number + 1
print(total)
10
# This function should return the number of scores at least 70.
# Find and fix the bug.
def count_passing(scores):
count = 0
for score in scores:
if score >= 70:
count = count + 1
return count
return count is indented too far. It returns during the first iteration instead of waiting until the entire list has been checked.
def count_passing(scores):
count = 0
for score in scores:
if score >= 70:
count = count + 1
return count
def ticket_price(age):
if age < 5:
return 0
elif age < 18:
return 10
else:
return 15
4, 5, 17, 18