Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
weight=0.0
weight=float(input("Enter a weight in kilograms:"))

else:
    if (weight >=5.0):
        print("Open Green gate.")
    elif(weight >=2.0):
        print("Open Red gate.")
    elif(weight <= 1.0):
        print("Open Yellow gate.")


What I have tried:

    else:
       ^
SyntaxError: invalid syntax
Posted
Updated 5-Feb-21 2:19am
v4
Comments
Richard MacCutchan 30-Jan-21 11:18am    
You cannot have else without a preceding if statement.
Mr Bobcat 5-Feb-21 8:21am    
And btw If user enters value between 2.0 and 1.0 this program will do nothing, address that range too.

Quote:
Hello can someone help me to solve this syntaxerror please ?

An "else:" is part of an "if: ... elif: ... else: ..." structure
Why so you have an "else" alone at this place ?
if no reason, just remove it.
[Update]
Quote:
IndentationError: unexpected indent

In Python, indentation matters, you can't indent for no reason, the "if" structure should be at same level as first line.
 
Share this answer
 
v2
Comments
beginner20060403 30-Jan-21 11:02am    
File "<string>", line 4
if (weight >=5.0):
^
IndentationError: unexpected indent
>
when I delete the else it gives me this error
can you help me
Patrice T 30-Jan-21 11:07am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
OriginalGriff 30-Jan-21 11:39am    
This one isn't willing to learn - check his other question. :sigh:
This is your code:
weight=0.0
weight=float(input("Enter a weight in kilograms:"))

else:
    if (weight >=5.0):
        print("Open Green gate.")
    elif(weight >=2.0):
        print("Open Red gate.")
    elif(weight <= 1.0):
        print("Open Yellow gate.")

You cannot write else without an "if" statement and there is no "if" statment before the first "else". Either write an "If" statement that does something or remove the "else" and just write it like this:

weight=0.0
weight=float(input("Enter a weight in kilograms:"))


if (weight >=5.0):
    print("Open Green gate.")
elif(weight >=2.0):
    print("Open Red gate.")
elif(weight <= 1.0):
    print("Open Yellow gate.")
 
Share this answer
 
Comments
Richard Deeming 5-Feb-21 8:57am    
As Patrice already explained last week.

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