Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin ty

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin ty

Post by answerhappygod »

Write a program with total change amount as an integer input
that outputs the change using the fewest coins, one coin type per
line. The coin types are dollars, quarters, dimes, nickels, and
pennies. Use singular and plural coin names as appropriate, like 1
penny vs. 2 pennies.
Ex: If the input is:
or less, the output is:
Ex: If the input is:
the output is:
Your program must define and call the following function. The
function exact_change() should return num_dollars, num_quarters,
num_dimes, num_nickels, and num_pennies.
def exact_change(user_total)
I got:
# Define your function here
def exact_change(user_total):
if user_total <= 0:
return print('no change')
elif user_total > 0:
num_dollars = user_total//100
user_total = user_total%100
num_quarters = user_total//25
user_total = user_total%25
num_dimes = user_total//10
user_total = user_total%10
num_nickels = user_total//5
user_total = user_total%5
num_pennies = user_total//1
return num_dollars, num_quarters,
num_dimes, num_nickels, num_pennies
if __name__ == '__main__':
input_val = int(input())
num_dollars, num_quarters, num_dimes, num_nickels,
num_pennies = exact_change(input_val)
if input_val <= 0:
print("no change")

else:
# calculate Dollars
num_dollars = input_val // 100
converted_dollar = str(num_dollars)
rem = input_val % 100
if num_dollars == 1:
print(converted_dollar + '
dollar')
elif num_dollars > 1:
print(converted_dollar + '
dollars')

# calculate Quarters
num_quarters = rem // 25
converted_quarter = str(num_quarters)
rem = rem % 25
if num_quarters == 1:
print(converted_quarter + '
quarter')
elif num_quarters > 1:
print(converted_quarter + '
quarters')
# calculate Dimes
num_dimes = rem // 10
converted_dime = str(num_dimes)
rem = rem % 10
if num_dimes == 1:
print(converted_dime + ' dime')
elif num_dimes > 1:
print(converted_dime + ' dimes')

# calculate Nickels
num_nickels = rem // 5
converted_nickel = str(num_nickels)
rem = rem % 5
if num_nickels==1:
print(converted_nickel + '
nickel')
elif num_nickels > 1:
print(converted_nickel + '
nickels')

# calculate Pennies
num_pennies = rem
converted_penny = str(num_pennies)
if num_pennies == 1:
print(converted_penny + ' penny')
elif num_pennies > 1:
print(converted_penny + ' pennies')
but i get an error code
Traceback (most recent call last): File "main.py", line 20, in
<module> num_dollars, num_quarters, num_dimes, num_nickels,
num_pennies = exact_change(input_val) TypeError: cannot unpack
non-iterable NoneType object
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply