Think of a list like a queue of passengers at the airport passport control. The system cannot process 50 passports at once. The officer (your for loop) has to say "Next!", take one passport, check it, and only then say "Next!" again. We process a list item by item.
Please write a function named count_fruits, which takes a list of strings as its argument. The function does not return anything. It should just use a for loop to print a message for each fruit in the list.
Here is an example of the function at work. Notice how we use the main block (the kitchen!) to test it:
if __name__ == "__main__":
fruits_list = ["apple", "banana", "cherry"]
count_fruits(fruits_list)
Here is a fruit: apple Here is a fruit: banana Here is a fruit: cherry
for item in list:. You don't need to do any complex math here, just focus on letting the loop do the work one by one.
Please write a function named speed_radar, which takes a list of integers (representing speeds in km/h) as its argument. The function should loop through the list and print a checking message for each speed.
An example of the function at work when called inside your main block:
if __name__ == "__main__":
speeds = [50, 120, 90]
speed_radar(speeds)
Checking speed: 50 km/h Checking speed: 120 km/h Checking speed: 90 km/h
Writing a function starting with def is just like writing a cake recipe in a book. Writing it doesn't magically bake the cake. The if __name__ == "__main__": block is the kitchen. This is where you grab your ingredients (create a variable), turn on the oven (call the function), and eat the cake (print the result).
Here is a function that is already written for you. Your job is to write the testing block to actually use it.
def add_two(number):
return number + 2
# Write your testing block down here!
In your testing block, you should:
1. Create a variable named my_number and assign it the value 10.
2. Call the function add_two using your variable as an argument.
3. Store the returned value in a new variable called test_result.
4. Print test_result.
12
NB: Don't forget the exact syntax for the main block: if __name__ == "__main__":
Everything created inside a def block (including its parameters) is locked in a secret room. The main block outside is completely blind to what happens in there. It doesn't know the names of the variables inside.
Look at the following code. It is intentionally broken and will crash.
def double_number(number):
room_result = number * 2
return room_result
if __name__ == "__main__":
my_digit = 5
print(double_number(number))
print(room_result)
Please fix the code inside the if __name__ == "__main__": block so that it correctly passes my_digit to the function, and correctly prints the returned result without crashing.
number and room_result. But those only exist inside the secret room! The main block only knows about my_digit.
A very common mistake is confusing print() and return. print() is just displaying text on a screen. It's like a vending machine showing you a picture of a soda. return is the machine actually dropping the physical soda into your hands so you can drink it, store it, or mix it later.
If a function only prints instead of returning, the main block receives absolutely nothing (it receives None).
Here is a program where the programmer wanted to calculate the price of a flight with taxes, and then apply a discount code on it in the main block. But it crashes with an error: TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'.
def add_taxes(price):
total = price + 50
print(total) # Uh oh...
if __name__ == "__main__":
flight_price = 200
# We try to get the total from the function
final_price = add_taxes(flight_price)
# We apply a 20 bucks discount
discounted_price = final_price - 20
print("You will pay:", discounted_price)
Please fix the add_taxes function so the main block receives the value and doesn't crash.
final_price. But because the function used print instead of giving back the value, final_price is empty (None). You can't subtract 20 from nothing! Swap that print for a return inside the function.