This program will ask the user for the number of half-dollars, quarters, dimes, nickels, and pennies s/he has and then compute the total value. The total value of the change should be shown both as the total number of cents, and then as the separate number of dollars (i.e., a whole number) and cents remaining (also as a whole number). I need help trying to get the "final total value" of this loop, ive been stuck on this for some time now.
def getCoin(coinType) :
coinval = -1
while coinval < 0:
try:
coinval = int(input(coinType))
if coinval < 0:
print( "Coin counts cannot be negative. Please re-enter.")
except ValueError:
print("Illegal input. Value converted to zero")
coinval = -1
#challenge for class on 8/30: keep asking user for value until good one is given: >=0
return coinval
print("welcome to the change calculator")
print()
choice= input("do you have change? (Y/N)")
while choice.upper() =="Y":
print("Your choice was:" choice)
hd = getCoin(" # Half-Dollars?")
q = getCoin(" # Quarters?")
d = getCoin(" # Dimes?")
n = getCoin(" # Nickels?")
p = getCoin(" # Pennies?")
totalvalue = (hd * 50) (q * 25) (d * 10) (n * 5) p
dollars = totalvalue //100 #integer division
cents = totalvalue % 100 #modulus gives just the remainder
print("you have " str(totalvalue) "cents.")
print(" this is " str(dollars) "dollars and " str(cents) "cents")
choice = input("do you have more change? (Y/N): ")
print("Thanks for using the change calculator")
CodePudding user response:
Create a separate variable outside the loop to keep track of the sum of all the money every time the user decides to press "Y":
final_total_value = 0
choice= input("do you have change? (Y/N)")
while choice.upper() =="Y":
...
totalvalue = (hd * 50) (q * 25) (d * 10) (n * 5) p
...
final_total_value = totalvalue
...
choice = input("do you have more change? (Y/N): ")
print("Final total value " str(final_total_value) "cents.")