My sample script is from a project did I did in a computer science class a couple years ago. I modified a little bit by importing argparse and adding in a useful help message. This script takes an integer that represents a number of cents as an input and uses an algorithm to determine the best combination of common American coins that would be needed to generate this number, using as few coins as possible. This script is perfect for determining how to make exact change for any positive number of cents.
Format to run the script:
python change_calculator.py integerOfYourChoice
import argparse
def change(amount):
if amount < 0:
raise ValueError('It\'s impossible to have a negative number of cents. Please try again.')
#The amount can't be negative
= dict() #This dictionary keeps track of the number of each American coin
change_count = int(amount/25)
quarters -=(quarters * 25)
amount= int(amount/10)
dimes -=(dimes * 10)
amount= int(amount/5)
nickels -=(nickels * 5)
amount= amount
pennies
'Quarters'] = quarters
change_count['Dimes'] = dimes
change_count['Nickels'] = nickels
change_count['Pennies'] = pennies
change_count[return change_count
if __name__ == '__main__':
= argparse.ArgumentParser(description='Find the combination of the smallest number of common American coins (quarters, dimes, nickels, and pennies) that add up to the given number of cents. The number of cents needs to be represented by an integer and must be positive.')
parser 'amount', type=int, help="The total number of cents (integer)")
parser.add_argument(= parser.parse_args()
args print("The number of quarters, dimes, nickels, and pennies needed to generate exact change: ", change(args.amount))