简体   繁体   中英

How can I get the right calculations for the total of the coin change calculator?

I am new at programming and I want to get better at it. I am working on a change calculator that accepts five arguments(dollars, quarters, dimes, nickels, pennies) and calculates the total amount of change. When I put change.py in the command line followed by 1 1 1 1 1 I am supposed to get 1.41 as the total but I am getting 1.95. Can anyone help with this? Thank you!

import sys

   
def change(dollars,quarters,dimes,nickels,pennies):
 
    dollars = int(float(dollars))
    quarters = int(float(quarters))
    dimes = int(float(dimes))
    nickels = int(float(nickels))
    pennies = int(float(pennies))

    total = (dollars * 100) + (quarters * 0.25 ) + (dimes * 0.10) + (nickels * 0.5) + (pennies * 0.1)

dollars = sys.argv[1]
quarters = sys.argv[2]
dimes = sys.argv[3]
nickels = sys.argv[4]
pennies = sys.argv[5]

      

print('The total value of your change is ',(total)) 

change(dollars,quarters,dimes,nickels,pennies)

IndexError                                Traceback (most recent call 
last)
<ipython-input-10-6fd4ec30e3dc> in <module>
 16 dollars = sys.argv[1]
 17 quarters = sys.argv[2]
 ---> 18 dimes = sys.argv[3]
 19 nickels = sys.argv[4]
 20 pennies = sys.argv[5]

 IndexError: list index out of range

With the code you have you need to ensure you are passing those arugments in the command line when you run your code:

python coinprogram.py 1 2 3 4 5

I also noticed the change is not being calculated correctly try this out:

import sys


dollars = sys.argv[1]
quarters = sys.argv[2]
dimes = sys.argv[3]
nickels = sys.argv[4]
pennies = sys.argv[5]


def change(dollars, quarters, dimes, nickels, pennies):

    dollars = int(float(dollars))
    quarters = int(float(quarters))
    dimes = int(float(dimes))
    nickels = int(float(nickels))
    pennies = int(float(pennies))

    return ((dollars * 100) + (quarters * 25) + (dimes * 10) + (nickels * 5) + (pennies * 1)) / 100


total = change(dollars, quarters, dimes, nickels, pennies)
print('The total value of your change is ', (total))

if you would prefer to get user input within the program then you could do something like this instead:

import sys


dollars = input("Enter dollars: ")
quarters = input("Enter quarters: ")
dimes = input("Enter dimes: ")
nickels = input("Enter nickels: ")
pennies = input("Enter pennies: ")


def change(dollars, quarters, dimes, nickels, pennies):

    dollars = int(float(dollars))
    quarters = int(float(quarters))
    dimes = int(float(dimes))
    nickels = int(float(nickels))
    pennies = int(float(pennies))

    return ((dollars * 100) + (quarters * 25) + (dimes * 10) + (nickels * 5) + (pennies * 1)) / 100


total = change(dollars, quarters, dimes, nickels, pennies)
print('The total value of your change is ', (total))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM