x = 10
y = "20"
print(str(x) + y)
1020 (Not 30, and not an error because x was cast to a string)
user_response = input("Do you want to play?")
if user_response == "yes"
print("Great!")
Missing colon after the condition:
if user_response == "yes":
Write a line of code that asks the user "How are you?" and saves their typed response to a variable called mood.
mood = input("How are you? ")
If colors = ["red", "blue", "green"], what code do you write to add "yellow" to the list?
colors.append("yellow")
What Python keyword do you use to define and start creating your own custom function?
def
items = ["apple", "banana"]
items.append("cherry")
print(items[1])
banana (Because indexing starts at 0, so banana is index 1)
while True
print("looping")
break
Missing colon after True:
while True:
Write an if statement that checks if a variable named user_input is equal to "help" (in all lowercase).
if user_input == "help":
If colors = ["red", "blue", "green"], what code do you write to print out just the word "red" from the list?
print(colors[0])
If you want a function to send a value back to the main program so you can save it in a variable, what keyword must you use at the end of the function?
return
words = ["chatbot", "AI", "python"]
for w in words:
if len(w) < 3:
print(w)
AI (It's the only string with a length less than 3)
points = 100
print("You have " + points + " points.")
Cannot concatenate int to string. Must cast points:
print("You have " + str(points) + " points.")
(or use an f-string:
print(f"You have {points} points.")
)
Write a line of code that takes a variable age (which contains a string like "15") and converts it into a whole number, saving it to a variable called numerical_age.
numerical_age = int(age)
Write a for loop that prints the numbers 0, 1, 2, and 3.
for i in range(4):
print(i)
Look at this code snippet. What will print to the screen when you run it?
def add_five(num):
return num + 5
add_five(10)
Nothing! (A classic beginner trap—the function calculates 15 and returns it, but because there is no print() statement, nothing shows up on the screen!)
count = 1
while count < 5:
count = count + 2
print(count)
5 (Loop steps: count starts at 1. Loops: becomes 3. Loops: becomes 5. 5 is not less than 5, loop exits, prints 5)
for i in range 5:
print(i)
Missing parentheses on range:
for i in range(5):
Write an if statement that checks if the player's score is greater than 50 AND they have a variable has_ticket equal to True.
if score > 50 and has_ticket == True:
(or if score > 50 and has_ticket:)
If inventory = ["Sword", "Shield", "Potion"], write a loop that prints out each item in the inventory one by one.
for item in inventory:
print(item)
What does this code print?
def double_it(x):
return x * 2
val = double_it(3) + double_it(4)
print(val)
14 (double_it(3) returns 6; double_it(4) returns 8; 6 + 8 = 14)
def calculate(val):
num = val * 2
return num
result = calculate(5)
print(num)
NameError (Because num only exists inside the local scope of the function! Outside the function, it doesn't exist)
def say_hello(user_name)
print("Hello " + user_name)
Missing colon at the end of the function definition:
def say_hello(user_name):
Use string concatenation (+) to write a print statement that prints: Welcome back, [name]! Your level is [level]. (Assume both name and level variables are already strings).
print("Welcome back, " + name + "! Your level is " + level + ".")
Write code that checks if the item "Map" is not currently inside a list called inventory.
if "Map" not in inventory:
(or if not "Map" in inventory:)
What type of error will Python throw if you define a function like def greet(name): but then try to call it by writing greet() with nothing inside the parentheses?
TypeError (Specifically: "missing 1 required positional argument")