I am a beginner in computer programming and need help with this problem: Create a script that prompts the user to provide the number of pennies, nickels, dimes, and quarters. Display back the value of the coins in dollars and cents.

Here is my code so far, everything is good except it doesn't properly display the number of cents left over. For example; if the total is \$1.76 it will say "you have 1 dollar and 1 cent(s). Thanks so much for any tips/help.

``````def main():
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)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)

print("Total =\$" , total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")

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

def get_dollars(pennies, nickels, dimes, quarters):
total_value = get_dollars(pennies, nickels, dimes, quarters)
dollars = format(total_value // 1, ".0f")
return dollars

def get_left_over_cents(pennies, nickels, dimes, quarters):
total_value = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = total_value
return left_over_cents

main()
``````

## Recommended Answers

Logically, dollars is just the floor(total/100) and the cents is total modulo dollars. Of course, you get in trouble when dollars is 0 so just do a check and make it modulo 1 instead. I would also do a check to make sure those ints are unsigned otherwise you get …

## All 3 Replies

I would remove `get_dollars()` and `get_left_over_cents()`. You can write at
line 13

``````total_value = get_total(pennies, nickels, dimes, quarters)
dollars = int(total_value)
left_over_cents = int(100 * (total_value - dollars))
``````

It seems to me that calling `get_dollars()` instead of `get_total()` at line 29 and 34 is a mistake.

Logically, dollars is just the floor(total/100) and the cents is total modulo dollars. Of course, you get in trouble when dollars is 0 so just do a check and make it modulo 1 instead. I would also do a check to make sure those ints are unsigned otherwise you get other math errors.

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
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.