First off, ignore my non-PEP-8 compliant coding, and just change them as needed.
I left your code intact up to and including line 13,total_value = get_total(pennies, nickels, dimes, quarters)
I eliminated your get dollars and getcents functions, as you can get those with one short line using divmod
. divmod
gives you both the quotient and modulus (remainder) of the desired division. I recommend you study up on modulus and quotient, as they are very handy, and oft used. So oft used that Python introduced the combo keyword divmod
.
I then switched out your last print lines to the new future compatible format.{:,.2f}
is to use comma separators and 2 significants after the decimal. {:,}
is just use comma separators. See this link for more info on formatting:
https://www.daniweb.com/software-development/python/code/492854/another-visit-to-string-formatting-python
pennies = int(input("Enter pennies:"))
nickels = int(input("Enter nickels:"))
dimes = int(input("Enter dimes:"))
quarters = int(input("Enter quarters:"))
print("You entered:")
print("\tPennies:" , pennies)
print("\tNickels:" , nickels)
print("\tDimes:" , dimes)
print("\tQuarters:" , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
v_Dollars, v_Cents = divmod(total_value, 1)
print("Total = ${:,.2f}".format(total_value))
print("You have {:,} dollar(s) and {:.2f} cent(s)".format(int(v_Dollars), v_Cents))
def get_total(pennies, nickels, dimes, quarters):
pennies = (.01 * pennies);
nickels = (.05 * nickels);
dimes = (.10 * dimes);
quarters = (.25 * quarters);
total_value = pennies + nickels + dimes + quarters
return total_value