else the else is a bit of a niche feature. What should happen if, for example, they enter a negative number? By the way, although python does support try. You can also do other validation at this stage. It's a good habit to always assume user input is broken, and give them an indication of what's going wrong. One good thing here is checking that the user input is a number. Return float(input("Please enter your taxable income: ")) Here is one alternative that removes a few unnecessary parts.
#Retirement calculator python program code#
There are a lot of ways you could code this sort of function. The only thing I do not like is int, as it is perfectly reasonable to earn a fractional unit of the currency. But until then I would leave the explicit if/elif/else statements in previous suggestion. If the original code ever gets more complex this is how I would try and simplify it. Note that this code is a bit denser than the original, and I don't know if I would recommend using a loop. As an example, we could check each cutoff is bigger than the last. We should probably include some sanity checks incase somebody incorrectly changes values. Tax = (income - previous_cutoff) * percent + additive # If we get here we never found a bracket to stop in # The final bracket, use if the income is bigger than any cutoffįor cutoff, percent, additive in tax_brackets: # S is reused from the previous iteration of the loop I'll leave the details out as there are a few features of python that might be new to you, and are worth looking up yourself. Since this repeated code now looks a bit easier to manager, let's turn it into a loop. S, P, A = 0, 0, 0 # Values picked so the tax amount is always 0.
![retirement calculator python program retirement calculator python program](https://images.hired.com/company_photos/34952/pictures/1556740684/square.jpg)
While this doesn't look any better now, it will be beneficial to explore this path. We could change the code to first figure out the tax bracket the income falls into, then compute the tax amount. At each tax bracket we subtract an amount, multiply by a percentage, and add back an amount tax = (income - S) * P + A If we look at how the tax is computed, the pattern is very clear.
![retirement calculator python program retirement calculator python program](https://i.ytimg.com/vi/I-7gn7wfzdw/maxresdefault.jpg)
The advantage of this is that your code for working out how much tax is owed is easy to use in another python module. I would lay it out like this def get_income():
![retirement calculator python program retirement calculator python program](https://djangostars.com/blog/uploads/2018/12/Cover-V8.png)
You can make this explicit with well named functions. You have three clear boundaries and have split the work appropriately. The idea behind the code can be summarized as Get the income from user input I would suggest you use an IDE or a linter, both of which will point out syntax problems in the code. Print("you owe", tax, "dollars in tax!" ) Print("Sorry, I didn't understand that please enter taxable income as a number") Income = int(input("Please enter your taxable income: ")) Given the bad indentation of the original code, I will assume you meant to post code that looks like this while True: