Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
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:

Can someone help me with the code, please?
thank you
Posted
Updated 29-Jan-21 20:59pm
v4
Comments
Patrice T 29-Jan-21 4:40am    
Give error exact message, and a larger piece of code.

1 solution

In Python, the language creators made a very odd - and silly - decision. Whitespace, and particularly indentation is significant.

Indentation defines where blocks of code start and stop - all data indented to the same or greater level is part of the same block.
So your code has an if statement that is followed by a large block of code:
Python
else:
    if (weight >=5.0):
        <<< Code block starts here
        print("Open Green gate.")
        elif(weight >=2.0):
            print("Open Red gate.")
            elif(weight <= 1.0):
                print("Open Yellow gate.")
        <<< Code block ends here
Which means that the elif doesn't match up to the if above it, and the system has no idea what to do with it - and you get an error message.
Probably, you meant this:
Python
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.")
But you need to check all of that very carefully to be sure!

And yes, it;'s silly: because spaces and TAB characters are both whitespace - and they look the same in many, many editors but are not the same as far as Python is concerned. I can see why they did it, but ... still a very silly decision that causes a heck of a lot of problems. Whatever editor you use, make sure it has "Replace tabs with spaces" set to "ON" or it will bite you, and bite you hard!


Quote:
Hello
I don't understand why the if statement needs to match up the elif statement? can you please explain to me?
thank you


If I say to you
"Otherwise, if they have white eggs, get six"
Then you have to infer I mean you to go to the shop, and perhaps get me some eggs. But you can't really understand what to do - because the instructions aren't at all clear.

If I say:
"Go to the shop. If they have brown eggs, get a dozen. Otherwise, if they have white eggs, get six. Otherwise, get me a bar of chocolate."
Then you understand what you have to do.

Your code has to do the same, so the system can't work out that it has to go to the shop at all - and in Python indentation is the key to that.
Your code version is like this:
Go to the shop.
If they have brown eggs, 
   get a dozen. 
   Otherwise, if they have white eggs, 
      get six. 
      Otherwise, 
         get me a bar of chocolate.

What you need to do is use the indentation to "match up" the various parts of the code:
If they have brown eggs, 
   get a dozen. 
Otherwise, if they have white eggs, 
   get six. 
Otherwise, 
   get me a bar of chocolate.
Now the system can tell that "Otherwise, if they have white eggs, get six." is meant to work with "If they have brown eggs, get a dozen." because the two chunks of text are indented to the same level.

Does that make sense?
 
Share this answer
 
v2
Comments
CPallini 29-Jan-21 4:50am    
5.
beginner20060403 29-Jan-21 11:40am    
Hello
I don't understand why the if statement needs to match up the elif statement? can you please explain to me?
thank you
OriginalGriff 29-Jan-21 12:19pm    
Answer updated.
beginner20060403 29-Jan-21 15:19pm    
thank you
beginner20060403 29-Jan-21 15:19pm    
thank you

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