SyntaxError: invalid syntax, python3 - python

income=float(input("enter the annual income: ")
if income<85528:
tax=(income-556.02)*0.18
else:
tax=(income-85528)*0.32+14839.02
tax=round(tax,0)
print("the tax is: ", tax, "thalers")
Why does it keep giving an error on line 2?

You have a lacking parenthesis in the 1st line: ")". It can be fixed by:
income = float(input("enter the annual income: "))
Complete program:
income = float(input("enter the annual income: "))
if income < 85528:
tax = (income - 556.02) * 0.18
else:
tax = (income - 85528) * 0.32 + 14839.02
tax = round(tax, 0)
print("the tax is: ", tax, "thalers")
Returns:
enter the annual income: 435
the tax is: -22.0 thalers

Related

Is there a way to use two format codes in an fstring in Python?

I'm building a basic program to calculate taxes in the UK. The code looks like this:
income = int(input("Income: "))
taxable = income - 12570
if income < 12_571:
print(f"You pay no tax.
Your income is £{income:.2f}")
elif income < 50_271:
afterTax = taxable * 0.8
realIncome = afterTax + 12570
print(f"You pay 20% tax.
Your income is £{realIncome:.2f}")
elif income < 150_000:
afterTax = taxable * 0.6
realIncome = afterTax + 12570
print(f"You pay 40% tax.
Your income is £{realIncome:.2f}")
else:
afterTax = taxable * 0.55
realIncome = afterTax + 12570
print(f"You pay 45% tax.
Your income is £{realIncome:.2f}")
`
When I run it, I get this:
Income: 3555555555555555532 You pay 45% tax. Your income is £1955555555555561472.00
The problem is I either format it to have 2 decimal places with :.2f, or to have comas with :,. Is there a way to do both?

Display a list of items without using a list

I am trying to create a program that inputs the price and tax rate and returns the item number, price, tax rate, and item price in an orderly format.
Here is the criteria:
The user can enter multiple items until the user says otherwise.
It calculates and display the total amount of purchased items on the bottom.
The price gets a 3% discount if the total amount is 1000 or more
I am not allowed to use arrays to solve this problem and I must separate the inputs from the outputs on the screen.
An example output is shown here:
Enter the price: 245.78
Enter the tax rate: 12
Are there any more items? (y/n) y
Enter the price: 34
Enter the tax rate: 10
Are there any more items? (y/n) y
Enter the price: 105.45
Enter the tax rate: 7
Are there any more items? (y/n) n
Item Price Tax Rate % Item Price
====================================
1 245.78 12% 275.27
2 34.00 10% 37.40
3 105.45 7% 112.83
Total amount: $425.51
Here is my code so far:
price= float(input('Enter the price: '))
taxRate= float(input('Enter the tax rate: '))
itemPrice = price * (1 + taxRate/100)
acc= 0
while True:
query=input('Are there any more items? (y/n)')
if query== 'y':
price= float(input('Enter the price: '))
taxRate= float(input('Enter the tax rate: '))
itemPrice = price * (1 + taxRate/100)
print(itemPrice )
acc+=1
elif query=='n':
break
print ("Item Price Tax Rate% Item Price")
print ("=========================================")
print( (acc+1) , format(price,'10.2f'), format(taxRate,'10.2f') ,
format(itemPrice,'14.2f') )
for num in range(0,acc):
print (acc, price, taxRate, itemPrice)
The output is shown as this:
Enter the price: 245.78
Enter the tax rate: 12
Are there any more items? (y/n)y
Enter the price: 34
Enter the tax rate: 10
37.400000000000006
Are there any more items? (y/n)y
Enter the price: 105.45
Enter the tax rate: 7
112.8315
Are there any more items? (y/n)n
Item Price Tax Rate% Item Price
=========================================
3 105.45 7.00 112.83
2 105.45 7.0 112.8315
2 105.45 7.0 112.8315
I just have a hard time trying to do this without using an array.. can anyone be of assistance?
You could build a string as a buffer for the output?
Like this:
out = ""
acc = 0
totalAmount = 0
query = 'y'
while query == 'y':
price = float(input('Enter the price: '))
taxRate = float(input('Enter the tax rate: '))
itemPrice = price * (1 + taxRate/100)
acc += 1
totalAmount += itemPrice
out += f"{acc:>4} {price:9.2f} {taxRate:11.2f}% {itemPrice:13.2f}\n"
query = input('Are there any more items? (y/n) ')
print("Item Price Tax Rate% Item Price")
print("=========================================")
print(out)
print(f"Total amount: ${totalAmount:.2f}")
if totalAmount >= 1000:
print(" Discount: 3%")
print(f"Total amount: ${totalAmount*0.97:.2f} (after discount)")

how do i print one set of statements after my program goes through multiple if-else statements

The program asks for user income for tax purposes and based on the income entered, calculates the amount of tax owed based on a series of if-else statements. The question I have is, is it possible to have just one set of print statements at the end of the program once it is out of the if-else statements? Instead of attaching my two print statements to each if else statement?
taxRate = 0;
print("Please enter employee's annual taxable income ... CAD($) ")
income = eval(input())
# get the users income
# determine the amount of taxable income given three different tax rates
if income < 0:
print("You cannot enter a negative value for the salary")
elif 0 <= income < 33389.01:
taxrate = 0.108
# calculate taxable income
taxableIncome = income * taxRate
# print tax value to two decimal places and print and tax bracket
elif 33389.01 <= income < 72164.01:
taxrate = 0.1275
taxableIncome = income * taxRate
else: # income >= 72164.01
taxrate = 0.174
taxableIncome = income * taxRate
print("Employee's provincial tax value : CAD($) " + str(round(taxableIncome, 2)))
print("Employee's provincial tax bracket : " + str(taxRate * 100) + "% [$0 .. $33389.01)")
This was the code originally
taxRate = 0;
print("Please enter employee's annual taxable income ... CAD($) ")
income = eval(input())
# get the users income
# determine the amount of taxable income given three different tax rates
if income < 0:
print("You cannot enter a negative value for the salary")
elif 0 <= income < 33389.01:
taxrate = 0.108
# calculate taxable income
taxableIncome = income * 0.125
# print tax value to two decimal places and print and tax bracket
print("Employee's provincial tax value : CAD($) " +
str(round(taxableIncome, 2)))
print("Employee's provincial tax bracket : " + str(.125 * 100) + "% [$0
.. $33389.01)")
elif 33389.01 <= income < 72164.01:
taxrate = 0.1275
taxableIncome = income * .1625
print("Employee's provincial tax value : CAD($) " +
str(round(taxableIncome, 2)))
print("Employee's provincial tax bracket : " + str(.1625 * 100) + "% [$0
.. $33389.01)")
else: # income >= 72164.01
taxrate = 0.174
taxableIncome = income * .1775
print("Employee's provincial tax value : CAD($) " +
str(round(taxableIncome, 2)))
print("Employee's provincial tax bracket : " + str(.1775 * 100) + "% [$0
.. $33389.01)")
So with the one set of print statements at the end, what happens is the taxRate stays at 0 because of the assignment statement at the beginning of the program. The value stored in taxRate does change once inside the if-elif-else statements but goes back to 0 once outside of the if-elif-else statements and prints 0. I want to assign the correct taxRate for the one and only set of print statements at the end.
here it what you are looking for
# get the users income
print("Please enter employee's annual taxable income ... CAD($) ")
income = eval(input())
taxRate = 0
# determine if negative income was entered
if income < 0:
print("You cannot enter a negative value for the salary")
# determine appropriate tax rate to apply
else:
if 0 <= income < 33389.01:
taxRate = 0.108
taxableIncome = income * taxRate
elif 33389.01 <= income < 72164.01:
taxRate = 0.1275
taxableIncome = income * taxRate
elif income >= 72164.01:
taxRate = 0.174
taxableIncome = income * taxRate
# display tax rate and corresponding tax bracket
print("Employee's provincial tax value : CAD($) " + str('{:.2f}'.format(taxableIncome)) + "\n" +
"Employee's provincial tax bracket : " + str(taxRate * 100) + "% [$72164.01 .. )")
You might be able to add a \n command at the end of the line to add a new line, possibly eliminating the need for two statements.
That would look like this:
taxRate = 0;
print("Please enter employee's annual taxable income ... CAD($) ")
income = eval(input())
# get the users income
# variable declarations for tax income 3 tax rates
# determine the amount of taxable income given three different tax rates
if income < 0:
print("You cannot enter a negative value for the salary")
elif 0 <= income < 33389.01:
taxrate = 0.108
# calculate taxable income
taxableIncome = income * taxRate
# print tax value to two decimal places and print and tax bracket
elif 33389.01 <= income < 72164.01:
taxrate = 0.1275
taxableIncome = income * taxRate
else: # income >= 72164.01
taxrate = 0.174
taxableIncome = income * taxRate
print("Employee's provincial tax value : CAD($) " + str(round(taxableIncome, 2,"\n","Employee's provincial tax bracket : " + str(taxRate * 100) + "% [$0 .. $33389.01)")))
Try this:
print(
"Employee's provincial tax value : CAD($) %s \n
Employee's provincial tax bracket : %s % [$0 .. $33389.01" % (str(round(taxableIncome, 2)), str(taxRate * 100))
)

if statement ignored in python

I have the following code:
For some reason, the program ignores the 2nd 'if' statement.
Does anyone have any idea why, please?
#define function
def CalculateBasicPay (hours, rate):
pay = hours * rate
return pay
def CalculateOvertimePay (overtime_hours, overtime_rate):
overtime = overtime_hours * overtime_rate * 1.5
return overtime
#main program to get user input
hoursWorked = int()
if hoursWorked < 40:
converted_hours = float(input("Enter number of hours: "))
converted_rate = float(input("Enter your rate: "))
totalHours = CalculateBasicPay(converted_hours,converted_rate)
print("Your total pay is: £", totalHours)
if hoursWorked > 40:
converted_hours = float(input("Enter number of hours: "))
converted_rate = float(input("Enter your rate: "))
totalHours2 = CalculateOvertimePay(converted_hours,converted_rate)
print("Your total pay is: £", totalHours2)
----------
The output is only taking the 1st condition always:
Enter number of hours: 5
Enter your rate: 2
Your total pay is: £ 10.0
>>>
Enter number of hours: 50
Enter your rate: 2
Your total pay is: £ 100.0
-----------
I'm brand-new to python! So please be nice :)
Cheers :)
You should get the hours worked outside the if statement:
#define function
def CalculateBasicPay (hours, rate):
pay = hours * rate
return pay
def CalculateOvertimePay (overtime_hours, overtime_rate):
overtime = overtime_hours * overtime_rate * 1.5
return overtime
#main program to get user input
hoursWorked = float(input("Enter number of hours: "))
converted_rate = float(input("Enter your rate: "))
if hoursWorked < 40:
totalHours = CalculateBasicPay(converted_hours,converted_rate)
print("Your total pay is: £", totalHours)
if hoursWorked > 40:
totalHours2 = CalculateOvertimePay(converted_hours,converted_rate)
print("Your total pay is: £", totalHours2)
Your line hoursWorked = int() doesn't get an input from the user, it just creates an integer with the value 0.
You should replace it with something like:
hoursWorked = int(input("How many hours have you worked: "))

How to get rid of the parenthesis and comma in my output?

I am writing a code in python where I use different functions to calculate wage, federal tax, state tax, and net worth. Everything is fine except my output says, ('Your wage is: $', 989) instead of Your wage is: $989 I tried using +(wag) but it doesn't let me run it. How do I get rid of the parenthesis, comma, and quotation marks from the output? And how do I make the output have no decimal points? I am not using float, so I don't know why it's still giving me decimal points. Here's my code:
def Calculatewages():
wage = (work*hours)
return wage
def CalcualteFederaltax():
if (status== ("Married")):
federaltax = 0.20
elif (status== ("Single")):
federaltax = 0.25
elif status:
federaltax = 0.22
federaltax = float(wag*federaltax)
return federaltax
def Calculatestatetax():
if(state=="CA") or (state=="NV") or (state=="SD") or (state=="WA") or (state=="AZ"):
statetax = 0.08
if (state== "TX") or(state=="IL") or (state=="MO") or (state=="OH") or (state=="VA"):
statetax = 0.07
if (state== "NM") or (state== "OR") or (state=="IN"):
statetax = 0.06
if (state):
statetax = 0.05
statetax = float(wag*statetax)
return statetax
def Calculatenet():
net = float(wag-FederalTax-StateTax)
return net
hours = input("Please enter your work hours: ")
work = input("Please enter your hourly rate: ")
state = input("Please enter your state of resident: ")
status = input("Please enter your marital status: ")
print("**********")
wag = Calculatewages()
FederalTax = CalcualteFederaltax()
StateTax = Calculatestatetax()
Net = Calculatenet()
print("Your wage is: $" ,wag)
print("Your federal tax is: $",FederalTax)
print("Your state tax is: $",StateTax)
print("Your net wage is: $",Net)
For this part:
print("Your wage is: $" ,wag)
print("Your federal tax is: $",FederalTax)
print("Your state tax is: $",StateTax)
print("Your net wage is: $",Net)
you can rewrite it as this using string's format() method:
print("Your wage is: ${}".format(wag))
print("Your federal tax is: ${}".format(FederalTax))
print("Your state tax is: ${}".format(StateTax))
print("Your net wage is: ${}".format(Net))
This is useful as you can insert the value into any place in the string (wherever you put the curly brackets).
As for your decimal points problem you can use the built in round function like this:
round(float(variable), int(decimal_places))
for example:
round(1.43523556, 2)
will return 1.44
There are no quotes or parenthesis in the output in python 3.x, Check if you are running on python 2 or python 3. Looks like you are on python 2 by judging your output.
So change all your print statements like this
print "Your net wage is: $", wag # remove brackets
...
However, if you want it to run on python 3 your code doesn't run as you are multiplying 2 strings in this line
def Calculatewages():
wage = (work*hours) # <------ here
return wage
To fix this issue you must cast them into int and then your code should run without problems.
hours = int(input("Please enter your work hours: ")) # < ---- Cast to int
work = int(input("Please enter your hourly rate: ")) # < ---- Cast to int
state = input("Please enter your state of resident: ")
status = input("Please enter your marital status: ")
My output:
Please enter your work hours: 8
Please enter your hourly rate: 10
Please enter your state of resident: IN
Please enter your marital status: Single
**********
Your wage is: $ 80
Your federal tax is: $ 20.0
Your state tax is: $ 4.0
Your net wage is: $ 56.0
you can also use string's format() method:
print("Your wage is: ${}".format(wag))
print("Your federal tax is: ${}".format(FederalTax))
print("Your state tax is: ${}".format(StateTax))
print("Your net wage is: ${}".format(Net))
If you're running under python3.x, then with your code, it should print out without the parenthesis, and if you're running under python2.x, then to get rid of the parenthesis, you might wanna try:
print "Your wage is: $", wag

Categories