if-else not functioning

Question:

Here are some results:

screenshot of results

Wondering why my code won’t work. As you can see at the bottom, it does function (to a certain extent). My best estimate is that my logic is not sufficient for the task at hand. Please be patient with me because I’m new to this.

Here is my code:

user_input_change = int(input('Enter Integer: '))
if user_input_change >= 0:
    if user_input_change >= 100:
        if (user_input_change // 100) != 0:
            if (user_input_change // 100) > 1:
                print('{} Dollar(s)'.format(user_input_change // 100))
                user_input_change = (user_input_change % 100)
        else:
            user_input_change = (user_input_change % 100)
    if user_input_change < 100:
        if (user_input_change // 25) != 0:
            if (user_input_change // 25) > 1:
                print('{} Quarter(s)'.format(user_input_change // 25))
                user_input_change = (user_input_change % 25)
        else:
            user_input_change = (user_input_change % 25)
    if user_input_change <= 25:
        if (user_input_change // 10) != 0:
            if (user_input_change // 10) > 1:
                print('{} Dime(s)'.format(user_input_change // 10))
                user_input_change = (user_input_change % 10)
        else:
            user_input_change = (user_input_change % 10)
    if user_input_change <= 10:
        if (user_input_change // 5) != 0:
            if (user_input_change // 5) > 1:
                print('{} Nickel(s)'.format(user_input_change // 5))
                user_input_change = (user_input_change % 5)
        else:
            user_input_change = (user_input_change % 5)
    if user_input_change <= 5:
        if (user_input_change // 1) != 0:
            if (user_input_change // 1) > 1:
                print('{} Pennies'.format(user_input_change // 1))
            else:
                print('{} Penny'.format(user_input_change // 1))
else:
    print('No change')
Asked By: makarox

||

Answers:

The main problem with your code is that you check if (user_input_change // 25) > 1: and similar. Let’s look at the different if statements (for the second coin, but it applies to all coins):

  • if user_input_change < 100: this check is redundant; after the previous block, the value has to be < 100
  • if (user_input_change // 25) != 0: this is just a complicated way to say user_input_change >= 25
  • if (user_input_change // 25) > 1: accordingly, this means user_input_change >= 50, so if the value is between 25 and 50, you just do % 25 in the else, but never print the coin.

Long story short: You have way too many nested if statements. The following is enough:

user_input_change = int(input('Enter Integer: '))
if user_input_change > 0:
    if user_input_change >= 100:
        print('{} Dollar(s)'.format(user_input_change // 100))
        user_input_change = (user_input_change % 100)
    if user_input_change >= 25:
        print('{} Quarter(s)'.format(user_input_change // 25))
        user_input_change = (user_input_change % 25)
    if user_input_change >= 10:
        print('{} Dime(s)'.format(user_input_change // 10))
        user_input_change = (user_input_change % 10)
    if user_input_change >= 5:
        print('{} Nickel(s)'.format(user_input_change // 5))
        user_input_change = (user_input_change % 5)
    if user_input_change >= 1:
        print('{} Pennies'.format(user_input_change // 1))
else:
    print('No change')

Also, this can be shortened using a dict, or list of tuple, with the different values and coin names. You may also try divmod here:

coins = {100: "Dollar(s)", 25: "Quarter(s)", 10: "Dime(s)", 5: "Nickel(s)", 1: "Pennie(s)"}
user_input_change = int(input('Enter Integer: '))
if user_input_change > 0:
    for val, name in coins.items():
        amount, user_input_change = divmod(user_input_change, val)
        if amount > 0:
            print(f'{amount} {name}')
else:
    print('No change')
Answered By: tobias_k

A lot of things can be improved in your code.

  • The first if condition here is made redundant by the second (1 is bigger than 0).

     if (user_input_change // 1) != 0:
         if (user_input_change // 1) > 1:
    

    So you can instead do

     if (user_input_change // 1) > 1:
    
  • Adding redundant user_input_change = (user_input_change % 5) statements.

  • Dividing by 1 for the pennies.

  • Using "{}".format() instead of f-strings, likeso f"{}"

  • You may also use the shorthand assignment operator, e.g. in your case user_input_change = user_input_change % 1 becomes user_input_change %= 1

With all that in mind, here is a working version of your code.

change = int(input('Enter Integer: '))
    
if change > 0:
    if change >= 100:
        print(f"{change//100} Dollar(s)")
        change %= 100
    if change >= 25:
        print(f"{change//25} Quarter(s)")
        change %= 25
    if change >= 10: 
        print(f"{change//10} Dime(s)")
        change %= 10
    if change >= 5:
        print(f"{change//5} Nickel(s)")
        change %= 5
    if change > 1:
        print(f"{change} Pennies")
    else:
        print("1 Penny")
else:
    print("No change")
Answered By: Matey Krastev
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.