← Back to Functions
πŸ›’

Shop Calculator

5 of 6

Exercise 4: Shop Calculator

Your Task

Build a function for a Roblox shop that calculates the total cost of buying multiple items, with a bulk discount.

Write a function shop_total(price, quantity) that:

  • Takes the price of one item and how many the player wants to buy
  • Returns the total cost
  • If buying 5 or more, apply a 10% discount to the total

Starter Code

def shop_total(price, quantity): # Your code here pass # Test your function: print(shop_total(100, 3)) # Should print: 300 print(shop_total(100, 5)) # Should print: 450.0 (10% off 500) print(shop_total(200, 10)) # Should print: 1800.0 (10% off 2000) print(shop_total(50, 1)) # Should print: 50

Hints

Hint 1

First calculate total = price * quantity. Then check if quantity >= 5 to apply the discount.

Hint 2

10% discount means the player pays 90% of the total: total * 0.9

Solution
def shop_total(price, quantity): total = price * quantity if quantity >= 5: total = total * 0.9 return total

Bonus

Write a function receipt(item_name, price, quantity) that prints a receipt like:

--- Receipt ---
Item: Diamond Sword
Price: 100 each
Quantity: 5
Total: 450.0 (10% discount applied!)

Use your shop_total function inside it!

Bonus Solution
def receipt(item_name, price, quantity): total = shop_total(price, quantity) print("--- Receipt ---") print(f"Item: {item_name}") print(f"Price: {price} each") print(f"Quantity: {quantity}") if quantity >= 5: print(f"Total: {total} (10% discount applied!)") else: print(f"Total: {total}")
πŸ’»

Try it yourself

Code: Shop Calculator

Loading Python runtime…
Python …
Loading...
Output
Click "Run" to execute your code...
ℹ️ About this Python environment

βœ… Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

βœ… Functions, classes, recursion, print()

❌ No file system, subprocess, OS access, or network requests

❌ No pip install (all supported modules are pre-loaded)

⏱️ 5 second execution time limit