promotion image of download ymail app
Promoted

How do I combine the print statements into one?

In python please. 

Attachment image

1 Answer

Relevance
  • 1 month ago
    Favorite Answer

    You left off part of the code that seems to need replacing.  I'd expect another assignment statement and print() call in the "elif 39475 < user_input < 84200:" section to show the effective tax rate.  I'd also expect more sections to follow.

    The first thing I'd note is that your choice of variable names could be improved.  Start with line 8, where you input an integer called "user_input".  Yes, that's user input but what's important about it for the program and the user is that it's the 2019 income.  Try:

        income = int(input('What is your 2019 income?:  '))

    Next, you have subtle but definite error in your if/elif logic.  If the user's income is exactly 9700 or exactly 39475, and so on, NONE of those if/elif cases will run.  An income of 9700 is not less than 9700 and is not greater than 9700.  If your program is working otherwise, try it and see what I mean.

    You could change the lower bound test to use <= instead of < to fix that, but so long as you are testing the income ranges in increasing order, you never have to check the lower bound at all.  I'll use that idea below.

    Then, in the if/elif statements, you have variables named tax_1, tax_2, and tax_3.  Only one of them is ever used in one execution of that code and each one holds the same logical value: the tax liability.  Yes, it's computed differently depending on the income, but it's the same thing to the user.  How much tax does he/she owe?  You can use the same variable name rather than cluttering up the program with extra variables.  Doing this also lets you separate out the print statement from the calculation of the value:

         if income < 9700:

              tax = 0 + (income - 0) * 0.10

          elif income < 39475:

              tax = 970 + (income - 9700) * 0.12

              # note that income can't be less than 9700 here!

         elif income < 84200:

              tax = 4543 + (user_input - 39475) * 0.22

         ...etc.

    Remember that an elif statement doesn't run--doesn't even perform the boolean test--unless all previous if/elif tests in the same group have failed.  In all of the elif statements, income must be >= 9700.  Otherwise the if would have run.  After the first elif, the income in any later elif (or else) statements must be >= 39475.  And so on...

    I left out the effective tax rate, since it's the same calculation--at least in the cases you've shown.  After all the if/elif/else stuff above, a single line will do:

         effective_tax_rate = 100.0 * tax / income

    And a single print() call for each computed value is all you need:

         print(f'Your tax liability is ${tax:,.2f}')

         print(f'Your effective tax rate is {effective_tax_rate:.1f}%')

Still have questions? Get your answers by asking now.