Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ive been stuck trying to figure out how to code the final total value

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")
    
    

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading