Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a calculator. I have elif statements with if statements inside. I think that is causing the error but it just made more errors.
elif o == "7":
    price=float(input("What was the original price of your purchase:"))

if price<0 :
    print("Invalid input")  
    quit()

taxrate=float(input("What is the tax rate of your area (the tax rate of texas is 0.0625):"))
if taxrate>15:
  print("Invalid input")
  quit()
if taxrate<0:
  print("Invalid input")
  quit()
print("The additional tax is:")
tax=print(price*taxrate)
total=float(total=(tax+price))
print("Your total purchase is:")
print(total)



elif o == "8":
h=float(input("Enter your height in meters: "))
w=float(input("Enter your Weight in Kg: "))
 
BMI=w/(h*h)
print("BMI Calculated is:  ",BMI)
 
if(BMI>0):
    if(BMI<=16):
        print("You are very underweight")
    elif(BMI<=18.5):
        print("You are underweight")
    elif(BMI<=25):
        print("Congrats! You are Healthy")
    elif(BMI<=30):
        print("You are overweight")
    else: 
        print("You are very overweight")
else:
    print("invalid input")

elif statement 7 does not have any errors but elif statement 8 and beyond all say "expected expression"

What I have tried:

I have tried indenting to create an expression, but it just creates more errors saying, "indent not expected" and "unindent not expected" I tried googling but i could not find anything
Posted
Updated 26-Jul-22 5:53am
v2

1 solution

You have got your indentations wrong, so the elif statements are not being recognised as connected to the previous if. It should be:
Python
elif o == "7":
    price=float(input("What was the original price of your purchase:"))

    if price<0 :
        print("Invalid input")  
        quit()
    
    taxrate=float(input("What is the tax rate of your area (the tax rate of texas is 0.0625):"))
    if taxrate>15:
      print("Invalid input")
      quit()
    if taxrate<0:
      print("Invalid input")
      quit()
    print("The additional tax is:")
    tax=print(price*taxrate)
    total=float(total=(tax+price))
    print("Your total purchase is:")
    print(total)
    


elif o == "8":
    h=float(input("Enter your height in meters: "))
    w=float(input("Enter your Weight in Kg: "))
     
    BMI=w/(h*h)
    print("BMI Calculated is:  ",BMI)
     
    if(BMI>0):
        if(BMI<=16):
            print("You are very underweight")
        elif(BMI<=18.5):
            print("You are underweight")
        elif(BMI<=25):
            print("Congrats! You are Healthy")
        elif(BMI<=30):
            print("You are overweight")
        else: 
            print("You are very overweight")
    else:
        print("invalid input")

It would be better to write functions that handle each specific choice rather than coding everything inline as you have done.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900