Get ready to join Mario, Luigi, Peach, and Yoshi in ten exciting programming adventures across the Mushroom Kingdom! In each challenge, youāll write your own function to solve a unique problem inspired by classic Mario Bros gamesāfrom escaping Bowserās Castle to powering up for a Bullet Bill Blitz.Ā
Throughout the tasks, youāll work with a shared list called powerups, which stores the names of Marioās special abilities, and a dictionary called powerup_strength, which keeps track of how powerful each item is.Ā
Youāll use input(), if statements, for and while loops, break, continue, try-except, and returnābut never print() inside your functions. Instead, youāll return your result and print it outside the function.Ā
These challenges are designed to help you practise all the Python structures youāve learned while having fun with familiar Mario characters and storylines! Task 1 has been completed for you.Ā
The following list and dictionary are used in most of the 10 challenges.
powerups = ["fire flower", "super star", "ice flower"]
powerup_strength = {
Ā Ā Ā Ā "fire flower": 40,
Ā Ā Ā Ā "super star": 100,
Ā Ā Ā Ā "ice flower": 30
}
Function name: welcome_hero()
Variables: hero_name, age, message
Princess Peach is hosting a welcome party for new Mushroom Kingdom heroes. Before you enter, Mario asks for your name and age. Once inside, he proudly shows you the current power-ups set on the golden table!
Write a function called welcome_hero(). Use input() to ask the user for their name and age. Use try-except to stop the program crashing if the age isnāt a number. Then build a welcome message. Use a for loop to add each power-up from the list, and include its strength from the dictionary. Return the final message using return.
ā Do not use print() inside your function. Instead, return the message. Then, outside the function, you can use:
msg = welcome_hero()
print(msg)
Function: Controls the welcome process and sends back the message.
List (powerups): Holds names of Marioās power-ups.
Dictionary (powerup_strength): Stores how strong each power-up is.
input(): Ask for name and age
try-except: Stop errors from bad age input.
if: Give a special message if under 8.
for loop: Add each power-up with its strength to the message.
return: Send back the message. Do not use print() inside the function.
Notice how the += is used to add sections of text to a string. This is a clever way of putting a string together with text fragments which are chosen in an if/else structure - lots of possible text outcomes can be created in this simple way.
powerups = ["fire flower", "super star", "ice flower"]
powerup_strength = {
Ā Ā Ā Ā "fire flower": 40,
Ā Ā Ā Ā "super star": 100,
Ā Ā Ā Ā "ice flower": 30
}
def welcome_hero():
Ā Ā Ā Ā try:
Ā Ā Ā Ā Ā Ā Ā Ā hero_name = input("What is your name, brave hero? ")
Ā Ā Ā Ā Ā Ā Ā Ā age = int(input("How old are you? "))
Ā Ā Ā Ā except:
Ā Ā Ā Ā Ā Ā Ā Ā return "Oops! Please enter a number for your age."
Ā Ā Ā Ā message = f"Welcome, {hero_name}!\n"
Ā Ā Ā Ā if age < 8:
Ā Ā Ā Ā Ā Ā Ā Ā message += "Youāre a bit young, but Yoshi will keep an eye on you!\n"
Ā Ā Ā Ā message += "Here are the power-ups ready on the golden table:\n"
Ā Ā Ā Ā
Ā Ā Ā Ā for item in powerups:
Ā Ā Ā Ā Ā Ā Ā Ā strength = powerup_strength[item]
Ā Ā Ā Ā Ā Ā Ā Ā message += f"- {item.title()} (Power: {strength})\n"
Ā Ā Ā Ā return message
# Example of how to use the function
msg = welcome_hero()
print(msg)
Function name: build_snack_name()
Variables: fruit, enemy, snack_name, count
Yoshi is creating power snacks for the team! You choose a fruit and a Mario enemy, and Yoshi will mix them into a snack name like āBanana Goombaā or āMelon Piranha.ā After that, he checks how many power-ups are strong enough for the next mission.
Make a function called build_snack_name(). Use input() to ask for a fruit and enemy. Combine them into a string. Then use a for loop to go through the powerups list and use an if statement to count how many have a strength greater than 35 (check using the dictionary). Return a message with the snack name and number of strong power-ups.
ā Donāt use print() in your function. Just return the message. Then outside the function:
result = build_snack_name()
print(result)
Function: Builds a snack name and gives a power-up summary.
List (powerups): Used to loop through power-up names.
Dictionary (powerup_strength): Used to check strength values.
input(): Ask for a fruit and enemy.
if: Count only strong power-ups.
for loop: Check each power-up one by one.
return: Send back a message (do not use print() inside the function).
Function name: check_lava_lift_energy()
Variables: energy, max_power, charging_status
Mario is trapped in Bowserās Castle and the lava is rising! To escape, he has to activate the Lava Lift platformābut it only works if his energy is high enough. If heās too tired, he needs to recharge. If heās full of energy, youāll need to find the strongest power-up to help him escape fast!
Create a function called check_lava_lift_energy(). Use input() to ask for Marioās energy level (0ā100). Use try-except to stop the program crashing if the user types something that isnāt a number. If energy is below 20, tell Mario to recharge. If energy is 80 or above, find the strongest power-up by checking the powerup_strength dictionary. Use a for loop to do this. Use a while loop to simulate Marioās charging if his energy is too low.
ā Do not use print() in the function. Just return the final message. Outside the function:
result = check_lava_lift_energy()
print(result)
Function: Controls the whole check and returns a message.
List (powerups): Loop through power-up names.
Dictionary (powerup_strength): Used to find the strongest power-up.
input(): Ask for Marioās current energy.
try-except: Catch if the user types something like ābananaā
if / elif / else: Decide whether to charge or escape.
for loop: Search for the power-up with the highest strength.
while loop: Simulate Marioās energy charging to 100.
break: Stop charging early if already full.
return: Return a message about Marioās energy and power-up choice.
Function name: guess_block_item()
Variables: guess, attempts, message
Marioās just bumped a glowing Mystery Block, and something rare is hiding inside! You have three chances to guess what it is. If you get it right, you unlock it and see its power. If you fail⦠it disappears forever into a Warp Pipe!
Write a function called guess_block_item(). Use a while loop to give the player three guesses. Inside the loop, ask for a guess using input(). Use an if statement to check if the guessed item is in the powerups list. If it is, use the powerup_strength dictionary to show its power and break the loop. If it isnāt, use continue to skip to the next guess. After 3 failed attempts, return a message saying the block vanished.
ā Use return, not print() inside your function. To show the message:
msg = guess_block_item()
print(msg)
Function: Handles the guessing game and returns a result.
List (powerups): Used to check if the guessed item exists.
Dictionary (powerup_strength): Reveals the strength of a correct guess.
input(): Ask the player for a guess.
while loop: Gives the user 3 tries.
if: Check if the guess is correct.
break: Exit early if correct.
continue: Skip to the next guess if wrong.
return: Return a message depending on success or failure.
Function name: find_luigi()
Variables: room, message, visible_items
Luigiās gone ghost-hunting again, and heās trapped inside a spooky Boo House! You need to find out what room heās in and check what power-ups are available to help Mario bust him out. If heās in the basement, be warnedāitās full of invisible Boos!
Create a function called find_luigi(). Use input() to ask what room Luigi is in. If the answer is "basement", use an if statement to include a spooky warning. Then loop through the powerups list using a for loop and create a message that includes each power-up and its strength (from the dictionary). Return the full message.
ā Do not use print() inside your function. Return the message and print it like this:
message = find_luigi()
print(message)
Function: Builds a message about Luigiās location and the available help.
List (powerups): Used to show what items are ready.
Dictionary (powerup_strength): Shows the strength of each item.
input(): Ask for the room Luigi is in.
if: Add a special warning if the room is the ābasementā.
for loop: Add each power-up and its strength to the message.
return: Send back the final message.
Function name: activate_defenses()
Variables: ready, defense_log, item
A wave of Bullet Bills is flying toward Mario! If you say yes, Mario will activate his power-up defense system. But weak power-ups like the ice flower wonāt be used. And if the super star gets activatedāitās game over for the enemies and the defense stops immediately!
Make a function called activate_defenses(). Ask the user with input() if Mario should activate his defenses. If the answer is āyesā, use a for loop to go through the powerups list. Use if to check if the strength is under 35āif so, use continue to skip it. If the item is āsuper starā, use break to end early. Build a string with each power-up used, and return it.
ā Use return, not print(), inside your function:
report = activate_defenses()
print(report)
Function: Runs the power-up defense system.
List (powerups): Loop through each item.
Dictionary (powerup_strength): Check how powerful each one is.
input(): Ask if defenses should be activated.
for loop: Go through the power-ups.
if: Skip weak ones and stop at super star.
continue: Skip power-ups weaker than 35.
break: Stop early if super star is used.
return: Return the full defense log.
Function name: escape_tunnel()
Variables: danger, countdown, escape_message
Toad and Mario are deep inside a secret underground tunnel when suddenlyārumble rumbleāit starts collapsing! If danger is real, theyāll drop everything and sprint to safety. Can you help them escape before the Thwomps crush them?
Create a function called escape_tunnel(). Ask the user with input() if thereās danger. If the answer is āyesā, use clear() to empty both the powerups list and the powerup_strength dictionary. Use a while loop to simulate a countdown escape (3, 2, 1). Use try-except in case the user types something strange. Return a message that either confirms escape or says āall clearā.
ā Remember: use return, not print() inside your function. To display the result:
message = escape_tunnel()
print(message)
Function: Handles the tunnel emergency.
List (powerups): Emptied if thereās danger.
Dictionary (powerup_strength): Emptied too.
input(): Ask if thereās danger.
if: Decide whether to escape.
while loop: Count down 3, 2, 1.
try-except: Catch strange input and avoid crashing.
return: Return an escape message or safety confirmation.
Function name: collect_powerup()
Variables: new_name, new_strength, message
Mario just found a shiny spinning shell glowing with mysterious energy! Is it a new power-upāor just a fancy Koopa shell? You decide what to call it and how powerful it is. If the power-up already exists, update it!
Create a function called collect_powerup(). Ask the user with input() for a new power-up name and its strength. Use try-except to catch errors if the user types something like ābananaā instead of a number. If the power-up name is already in the list, update its strength in the dictionary. If itās not in the list, add it to both the list and the dictionary. Return a message confirming what happened.
ā Donāt use print() in the function. Use:
update = collect_powerup()
print(update)
Function: Adds or updates a power-up.
List (powerups): Holds the names of all power-ups.
Dictionary (powerup_strength): Holds their strength values.
input(): Ask for name and strength.
if: Check if the power-up already exists.
try-except: Catch input errors for the strength value.
return: Return a message confirming the result.
Function name: sort_powerups()
Variables: sort_type, sorted_items, message
Marioās about to race in the Koopa Kart Grand Prix, but his power-up stash is a mess! Should he sort them by name for quick accessāor by strength to grab the strongest first? You decide, but if you type something silly, heāll ask again!
Write a function called sort_powerups(). Ask the user with input() how they want to sort the power-ups: by "name" or "strength". Use a while loop to keep asking until the input is valid. Use a try-except block to catch any sorting errors. Then use a for loop to build a message showing the sorted power-ups with their strength from the dictionary. Return the message.
ā Donāt use print() inside your function. Do this instead:
sorted_list = sort_powerups()
print(sorted_list)
Function: Sorts power-ups and returns a summary.
List (powerups): Used to create a sorted version.
Dictionary (powerup_strength): Used to show each power-upās strength.
input(): Ask how the user wants to sort.
while loop: Repeat the question until a valid answer is given.
try-except: Prevent crashes from bad input.
for loop: Build the final sorted summary.
return: Return the sorted message.
Function name: start_final_battle()
Variables: nickname, castle, count, selected_items, message
Itās time for the final battle! Mario's charging toward Bowserās Castle. Whatās your hero nickname? Which castle will you storm? Choose how many power-ups to bringābut only strong ones will be used. When youāre ready, type āgo!ā to begin the battle!
Create a function called start_final_battle(). Use input() to ask for a nickname, castle name, and number of power-ups to bring. Use try-except to make sure the number is a valid integer. Use a for loop to select the number of power-ups from the list. Use if and continue to skip power-ups with a strength under 30. Then use a while loop to wait for the user to type āgo!ā and break out of the loop when they do. Return a mission summary.
ā Donāt use print() in your function. Instead:
summary = start_final_battle()
print(summary)
Function: Prepares for the final battle and returns the plan.
List (powerups): Used to choose which power-ups to take.
Dictionary (powerup_strength): Used to check power strength.
input(): Ask for nickname, castle, number of power-ups, and "go!" command.
try-except: Catch input errors for number of power-ups.
for loop: Select and filter power-ups.
if: Skip power-ups with low strength.
continue: Ignore weak items
while loop: Wait for the user to type "go!"
break: Exit the loop and start the battle.
return: Return the full mission summary.