[Python] Write a program with total change amount as an integer input and output the change...

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    This is a Lab activity in my intro to comp programming class. The prompt is as follows: Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies.

    This is the code I'm using:

    I'm getting an 8/10 because when I input "45" I get the output:

    1 Quarter

    2 Dimes

    No change.

    The answer has to be:

    1 Quarter

    2 Dimes.

    I have tried moving the if <= 0 print "no change" to the top and it's not fixing the issue. Please review and help me find my error(s). Thank you.

    # LAB: 3.33 Exact Change
    # Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line.
    # The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies.
    # Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies.
    # get_exact_change
    userInput = int(input())
    dollar_amt = userInput // 100
    userInput %= 100
    quarter_amt = userInput // 25
    userInput %= 25
    dime_amt = userInput // 10
    userInput %= 10
    nickel_amt = userInput // 5
    userInput %= 5
    penny_amt = userInput // 1
    userInput %= 1


    if dollar_amt == 1 :
    print(dollar_amt, "Dollar")
    elif dollar_amt > 1 :
    print(dollar_amt, "Dollars")
    if quarter_amt == 1 :
    print(quarter_amt, "Quarter")
    elif quarter_amt > 1 :
    print(quarter_amt, "Quarters")
    if dime_amt == 1 :
    print(dime_amt, "Dime")
    elif dime_amt > 1 :
    print(dime_amt, "Dimes")
    if nickel_amt == 1 :
    print(nickel_amt, "Nickel")
    elif nickel_amt > 1 :
    print(nickel_amt, "Nickles")
    if penny_amt == 1 :
    print(penny_amt, "Penny")
    elif penny_amt > 1 :
    print(penny_amt, "Pennies")

    else :
    if userInput <= 0 :
    print("No change")

    Continue reading...

Compartilhe esta Página