Home > Software engineering >  I am struggling to figure out why my code keeps outputting the wrong values
I am struggling to figure out why my code keeps outputting the wrong values

Time:03-17

in my current zybooks lab 3.13 the assignment asks to "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."

When I submit my work I am receiving '0/2' on 2 requirements. I need to figure out how to make it stop outputting 'No change' if there is a '0' in the integer. HALP!!!!!

bad output 1 bad output 2

here is my code:

user_input = int(input())

# Conditional math Block: Used to determine user input value in amount of dollars, quarters, dimes, nickles and pennies.
dollar = user_input // 100  # convert to dollars
user_input %= 100  # remainder after conversion

quarter = user_input // 25  # convert to quarters
user_input %= 25  # remainder after conversion

dime = user_input // 10  # convert to dimes
user_input %= 10  # remainder after conversion

nickel = user_input // 5  # convert to nickels
user_input %= 5  # remainder after conversion

penny = user_input

# Determines if user input is invalid.
if user_input <= 0:
    print('No change')

# Condition Block: Analyzes user input and assigns appropriate values.
if dollar > 1:  # Uses math block to determine if user input is equivalent to more than 1 Dollar.
    print(dollar, 'Dollars')
elif dollar == 1:  # Uses math block to determine if user input is equivalent to 1 Dollar.
    print(dollar, 'Dollar')

if quarter > 1:  # Uses math block to determine if user input is equivalent to more than 1 Quarter.
    print(quarter, 'Quarters')
elif quarter == 1:  # Uses math block to determine if user input is equivalent to 1 Quarter.
    print(quarter, 'Quarter')

if dime > 1:   # Uses math block to determine if user input is equivalent to more than 1 Dime.
    print(dime, 'Dimes')
elif dime == 1:  # Uses math block to determine if user input is equivalent to 1 Dime.
    print(dime, 'Dime')

if nickel > 1:   # Uses math block to determine if user input is equivalent to more than 1 nickel.
    print(nickel, 'Nickels')
elif nickel == 1:  # Uses math block to determine if user input is equivalent to 1 nickel.
    print(nickel, 'Nickel')

if penny > 1:  # Uses math block to determine how many Pennies' user input is equivalent to.
    print(penny, 'Pennies')
elif penny == 1:  # Uses math block to determine how many Pennies' user input is equivalent to.
    print(penny, 'Penny') ```

CodePudding user response:

You can print "No change" before the calculations.

user_input = int(input())
if user_input <= 0: 
    print('No change')
dollar = user_input//100
user_input %= 100
#the rest

CodePudding user response:

The crux of the problem is that you're doing this:

if user_input <= 0:
    print('No change')

after you have already reduced the value of user_input -- for example, this line:

user_input %= 10  # remainder after conversion

will reduce user_input to zero if it was any number ending in 0.

What you want to do instead is to test for the "no change" case either before you've started modifying the user input, or to test it based on all the various dollars, dimes, etc values you've accumulated.

FWIW, I'd suggest not storing all of these things in copy pasted variables in the first place. Here's an example of how to do the whole thing in a loop by defining all the coin types up front and then just repeating the operations with the appropriate values for each coin:

total = int(input())
if total <= 0:
    print("No change")
else:
    for value, s_name, p_name in [
        (100, "Dollar", "Dollars"),
        (25, "Quarter", "Quarters"),
        (10, "Dime", "Dimes"),
        (5, "Nickel", "Nickels"),
        (1, "Penny", "Pennies"),
    ]:
        qty = total // value
        if not qty:
            continue
        print(f"{qty} {p_name if qty > 1 else s_name}")
        total %= value
  • Related