Dispense Change - Python Programming Exercise

This Python program calculates the change for a purchase, using the largest possible coins or bills. The program prompts the user for the price of the item and the amount paid, then determines the change to be returned. The change is calculated by using the largest denominations first—starting with 100, followed by 50, 20, 10, 5, 2, and 1. This ensures that the fewest number of coins or bills is used. The program iterates through the denominations, subtracting the value from the change until no more can be given with a particular denomination. This program is an excellent way to practice using loops and conditional statements in Python. It also helps users understand how to break down problems into smaller steps, as the change is calculated by iterating through each denomination one by one. By the end of this program, the user will know how to give the correct change using the smallest number of coins or bills, making this a practical tool for everyday financial calculations.

 Category

Mastering Flow Control

 Exercise

Dispense Change

 Objective

Develop a Python program to give change for a purchase, using the largest possible coins (or bills). Suppose we have an unlimited amount of coins (or bills) of 100, 50, 20, 10, 5, 2, and 1, and there are no decimals. Therefore, the execution could be something like this:

Price? 44
Paid? 100
Your change is 56: 50 5 1
Price? 1
Paid? 100
Your change is 99: 50 20 20 5 2 2

 Example Python Exercise

 Copy Python Code
# Ask for the price and the amount paid
price = int(input("Price? "))
paid = int(input("Paid? "))

# Calculate the change
change = paid - price
denominations = [100, 50, 20, 10, 5, 2, 1]
change_list = []

# Loop through the denominations and calculate the necessary coins/bills
for denomination in denominations:
    while change >= denomination:
        change -= denomination
        change_list.append(denomination)

# Print the result
print(f"Your change is {paid - price}: {' '.join(map(str, change_list))}")

 Output

Case 1:
Price? 44
Paid? 100
Your change is 56: 50 5 1

Case 2:
Price? 1
Paid? 100
Your change is 99: 50 20 20 5 2 2

 Share this Python Exercise

 More Python Programming Exercises of Mastering Flow Control

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Error Handling

    This Python program prompts the user for two numbers and performs the division operation. It uses a try..except block to catch potential errors such as ...

  •  Positive and Negative Numbers

    In this exercise, you will develop a Python program that prompts the user to input a number and then determines if the number is positive or n...

  •  Multiply Unless Zero

    In this exercise, you will develop a Python program that prompts the user for a number. If the number is not zero, the program will ask for a second ...

  •  Division When Non-Zero

    In this exercise, you will develop a Python program that prompts the user for two numbers. The program will display their division if the second numb...

  •  Conditional Division (Using Else)

    In this exercise, you will develop a Python program that prompts the user for two numbers. The program will display their division if the second numb...

  •  Largest Among Three Values

    In this exercise, you will develop a Python program that prompts the user to input three numbers and then determines and displays the largest one. This ...