A function doesn’t have to return just one value. It can return several using a tuple or list.
This is useful when:
A function does a complex calculation.
You need different pieces of related information.
You want to reduce the number of function calls and keep your code clean.
We will deal with tuples a lot more, but for now, you just need to know that a tuple is essentially a list, which we can't change the values of
We access these the EXACT SAME WAY we access a list too!
import random
#This is a function which rolls a pair of dice
def roll_2_dice(numSides):
r1 = random.randint(1, numSides)
r2 = random.randint(1, numSides)
return r1, r2
# Since the function returns 2 things, I need to place
# the values in 2 variables! This is called UNPACKING
d1, d2 = roll_2_dice(6)
print(f"Your First Die was: {d1}")
print(f"Your Second Die was: {d2}")
roll_2_dice() takes 1 parameter, the number of sides of a die
it will roll 2 times, and store those values in r1 and r2
Since 2 things are returned, we need to UNPACK THOSE VALUES into 2 SEPARATE VARIABLES which are COMMA SEPARATED and IN-ORDER
We can then use those as we see fit
Example:
def get_hero( ):fn = "Bruce"ln = "Wayne"alias = "Batman"superpower = "Being Rich"return fn, ln, alias, superpower #Unpack the Tuple Returned:first, last, heroname, spower = get_hero()print(f"{first} {last} is {heroname} and is power is {spower}")Dr. Doofenshmirtz is building a brand-new -inator device! But he needs help coming up with the name, function, and unexpected side effects. Students will write a function that randomly generates a new invention, returning multiple values (name, purpose, and side effect).
1️⃣ The create_inator() function randomly selects a name, purpose, and side effect.
2️⃣ It returns these three values as a tuple.
3️⃣ The main program unpacks the tuple into three variables (name, purpose, effect).
4️⃣ It then prints out the hilarious invention and its unintended consequences.
Remember, the NAME of the Inator, Purpose, and Side Effect are all stored in the create_inator() function
You have to get one of each of them randomly
The function returns the tuple to be unpacked
Your Goal:
Weather data often comes with multiple related values. Returning them as a tuple lets you pass around a full snapshot of the forecast, which can be unpacked and used for display, analysis, or alerts without extra fuss.
You want to write a function called get_weather() that will return:
City Name
Determined from a random list:
BONUS: Write a function called get_city() which returns a city to get_weather()
Temperature
Generated Randomly from an appropriate set of values
BONUS: Write a function called get_temp() which will return a temperature to get_weather()
Weather Condition
Determined from a random list:
BONUS: Write a function called get_condition() which returns a weather condition to get_weather()
Sunny, Cloudy, Snowing, etc...
Wind Speed
Generated Randomly from an appropriate set of values
BONUS: Write a function called get_wind() which will return a temperature to get_weather()
Write a function called format_weather(city, temp, condition, wspeed) which
Formats the weather report in a pleasing manner
Additionally, you need to ask the user if they want to see the weather for another city, and stop when they say no
BONUS: Don't show the same city information more than once!!!
This is trickier than you think...
Now, lets look at how this is used in the real-world! A real-world application of functions returning values can be seen in an online shopping system. This program simulates a checkout system where:
✅ One function prints a receipt (no return)
✅ One function returns a single value (calculates tax)
✅ Two functions return multiple values (process an item & checkout total)
1️⃣ print_receipt_header() – Prints the store header (No return).
2️⃣ calculate_tax() – Calculates 8% tax on the total purchase (Single return).
3️⃣ process_item() – Selects a random product and its price (Multiple return).
4️⃣ checkout() – Generates 1-5 random items, calculates the subtotal, and returns the cart + total cost (Multiple return).
🔹 Task 1: Print the Store Header (No Return Function)
Create a function that prints a welcome message and a decorative receipt header.
This function does not return anything, it just displays information.
🔹 Task 2: Calculate Sales Tax (Single Return Function)
Create a function that takes the subtotal as an argument.
Calculate the tax (8% of the subtotal).
Return the tax amount to be added to the total price.
🔹 Task 3: Process a Single Item (Multiple Return Values)
Create a function that selects a random item from a list of products.
The function should also select the corresponding price of the item.
Return both the item name and its price.
🔹 Task 4: Checkout Process (Multiple Return Values)
Create a function that:
Selects 1 to 5 random items using the function from Task 3.
Stores the items and their prices in a list (or similar structure).
Calculates the subtotal of all selected items.
Returns the list of purchased items and the subtotal.
🔹 Task 5: Display Final Receipt
Call the functions in the correct order:
Print the receipt header.
Run the checkout process to get purchased items and subtotal.
Calculate the tax using the subtotal.
Display all items purchased, subtotal, tax, and final total in a well-formatted receipt.