I'm working on a new introduction to Python 3.0 course and there is no support - it is self study.
I am enjoying the simplicity of Python, but this course has been full of errors and I am starting to loose interest in it because the course texts are not as accurate as I would have liked.
The following is a lab taken from the course on python, which is what I am having difficulty with:
Estimated time 10-15 minutes Level of difficulty Easy/Medium Objectives Familiarize the student with: • using the if-else instruction to branch the control path; • building a complete program that solves simple real-life problems.
Scenario Once upon a time there was a land - a land of milk and honey, inhabited by happy and prosperous people. The people paid taxes, of course - their happiness had limits. The most important tax, called the Personal Income Tax (PIT for short) had to be paid once a year, and was evaluated using the following rule:
•if the citizen's income was not higher than 85,528 thalers, the tax was equal to 18% of the income minus 556 thalers and 2 cents (this was the so-called tax relief); •if the income was higher than this amount, the tax was equal to 14,839 thalers and 2 cents, plus 32% of the surplus over 85,528 thalers. Your task is to write a simple "tax calculator" - it should accept one floating-point value: the income. Next, it should print the calculated tax, rounded to full thalers. There's a function named round which will do the rounding for you - you'll find it in the skeleton code below. Note: this happy country never returns money to its citizens. If the calculated tax is less than zero, it only means no tax at all (the tax is equal to zero). Take this into consideration during your calculations. Look at the code below - it only reads one input value and outputs a result, so you need to complete it with some smart calculations. Test your code using the data we've provided.
My proposed solution is:
'''
If tax is less than or equal to 85,528 tax is 18% of income - 556.02. If tax is more than 85,528, tax is 14,839.02 plus 32% of surplus above 85,528.
'''
income = float(input("Enter the annual income: "))
if income <= 85528:
tax = (income-556.02)*0.18
# Brackets are to make it do those sums first
else:
tax = (income-85528)*0.32 + 14839.02
# Brackets are to make it do those sums first
tax = round(tax,0)
print("The tax is:", tax)
I should point out that the 1st line and the bottom 2 lines are provided for us, and so we cannot change them.
My problem is the 'if' line. I'm struggling to understand why it keeps throwing up an error when the number is an integer, even though the variable "income" will most likely be an integer, should I declare that the number to check to be greater than or equal to is a float?
I'm studying this subject and I only want to understand why this programme will not run in its current form. I suspect I am close and my only errors are the grammar of python, like maybe I should write this number as a float by adding a decimal and zero and declaring it a float maybe?
Please be patient with me - this is a new subject I am studying and I have also consulted a few books on this and none of the examples in the books have given me any idea about how to compare an input with an integer when the input is declared as a float.
Many thanks in advance for your replies.
income = float(input("Enter the annual income: "))
RépondreSupprimerif income <= 85528:
tax = income*0.18-556.02
else:
tax = (income-85528)*0.32 + 14839.02
if tax >= 0:
tax = round(tax,0)
else:
tax=0.0
print("The tax is:", tax, "thalers")
Немного неправильные вычисления, и нет проверки if tax<0
RépondreSupprimerncome = float(input("What is your annual income in thalers? "))
RépondreSupprimerif income <= 85528:
tax = (0.18 * income) - 556.02
else:
tax = (income - 85528) * 0.32 +14839.02
if tax < 0:
print("The tax is:", 0.0, "thalers")
else:
tax = round(tax, 0)
print("The tax is:", tax, "thalers")
Ce commentaire a été supprimé par l'auteur.
RépondreSupprimerincome = float(input("Enter the annual income: "))
RépondreSupprimerif (income <= 85528 and income > 0):
tax = (income*0.18)-556.02
elif income >85528:
tax = (income-85528)*0.32 + 14839.02
elif income >=0 :
tax = 0.0
tax = round(tax, 0)
print("The tax is:", tax, "thalers")
this is what i come up with, take a look
RépondreSupprimer----------------------------------------------------------
income = float(input("Enter the annual income: "))
specific_income = 85528
if income > specific_income:
surplus = income - specific_income
d_surplus = ((32/surplus*100)*surplus)
tax = d_surplus + 14839.2
if income < specific_income:
tax = (((18/specific_income*100)*specific_income)-556)
tax = round(tax, 0)
print("The tax is:", tax, "thalers")
Hi. Below is a simple solution to your problem. Python has the capability to compare an integer and a float value.
RépondreSupprimerincome = float(input("Enter the annual income: "))
if income <= 85528:
tax = (income * 0.18)-556.02
else:
tax = 14839.02 + (income - 85528)* 0.32
if tax <= 0.0:
tax = 0.0
else :
tax = round(tax, 0)
print("The tax is:", tax, "thalers")