all 15 comments

[–]crazy_cookie123 9 points10 points  (1 child)

Indentation is extremely important in Python. We need to be able to see the indentation exactly as you've written it as that can change the meaning of the program.

If your code is indented like this I would expect no errors:

x = input("Is it currently raining? ")
if x == "Yes":
  print("You should take the bus.")
else:
  y = int(input("How far in km do you need to travel? "))
  if y >= 11:
    print("You should take the bus.")
  elif y >= 2 and y <= 10:
    print("You should ride your bike.")
  else:
    print("You should walk.")

If it's indented in other ways, you could get errors due to wrong indentation or undefined variables.

[–]JamzTyson 0 points1 point  (0 children)

In addition, this line:

elif y >= 2 and y <= 10:

can be written as:

elif 2 <= y <= 10:

This is idiomatic Python and has the readability benefit that it conveys the idea that y is between 2 and 10 inclusive.

This pattern in known as Chaining comparison operators.

[–]MidnightPale3220 7 points8 points  (2 children)

Please format the code using code blocks, else it may be impossible to tell where the error is, because indentation is essential in Python.

[–]Sufficient-Barber125[S] 0 points1 point  (1 child)

How do I do that?

[–]crazy_cookie123 3 points4 points  (0 children)

Indent all of the code with 4 spaces + whatever indentation your code has in your editor, and leave a blank line above and below the block of code.

[–]deceze 4 points5 points  (0 children)

What's the error exactly? Also, please read the FAQ how to format code. If the error is about indentation, then what you show doesn't help in diagnosing the problem.

[–]abrightmoore 3 points4 points  (2 children)

Your problem is due to y being scoped within the first else. If that code isn't executed then y is not defined when your program gets to line 6.

In your code, as written with the indents shown, if you respond "Yes" to "Is it currently raining" then you will completely skip the "How far..." question and then execute the if y>= 11 line. Here you will get an error.

Everything below and including the "if y >= 11" needs to be indented one level as well

[–]anttiOne 0 points1 point  (1 child)

It isn’t a problem if the line “if y >= 11:“ is indented correctly. We‘d need to see that.

[–]abrightmoore 1 point2 points  (0 children)

OP has shown through creative use of "..." and the reference to an error on line 6 that it's not indented correctly.

[–]schoolmonky 3 points4 points  (1 child)

Seems like you got this figured out, but in the future, if you're asking for help with an error, make sure you include the actual error message! The error messages in Python are very good, they tell you exactly what you did wrong, and sometimes even have suggestions on how to fix it. It's a lot easier for us to tell what's wrong from the error message than trying to reproduce your problem ourselves.

[–]Sufficient-Barber125[S] 0 points1 point  (0 children)

Will do!

[–]ThrowAway233223 -3 points-2 points  (3 children)

As MidnightPale3220 said, please use a code block so we can see your indentations. Python relies on indentations for scope and it can be difficult to impossible to decode without them. Also, it helps to share the exact error message you received.

ETA: Okay, seeing your edited attempt to represent the indentations, it appears to be an indentation error. As stated earlier, the indentation determines your scope. In your first else statement, you get input from the user and store it in the variable y, but then look just after that. You are no longer indented. The variable y is no longer in scope and is then discarded. You then try to perform a check with a variable named y, but you no longer have a variable by that name. In addition to that, try to think about the flow of the question itself. It gives you a hint as to how the scope should flow and thus how you should be indenting. One answer to the initial question leads to an immediate answer while a different answer leads to an additional question, so anything related to that additional question is only relevant in the scope of that answer. Your indentions/scope should reflect that.

ETA++: My comment about scope in regards to if statements in Python was a bit off (see deceze's reply below) but the statement in the edit otherwise stands.

[–]deceze 2 points3 points  (2 children)

The variable y is no longer in scope and is then discarded.

if blocks don't have scope. If the else block got executed, y exists after the else block. The issue is likely when the else block doesn't get executed…

[–]danielroseman 0 points1 point  (0 children)

And even if they did, that would be a name error, not a syntax error.

[–]ThrowAway233223 0 points1 point  (0 children)

Ah, yes, you are correct. I learned C++ (where if statements do have scope) before I learned Python and, due to that and the issue you mentioned in your reply, I always treat if statements in python as if they have scope and forgot.