35

I have this code:

option == 1
while option != 0:
    print("MENU")
    option = input()
    print("please make a selection")
    print("1. count")
    print("0. quit")
    if option == 1:
        while option != 0:
            print("1. count up")
            print("2. count down")
            print("0. go back")
            if option == 1:
                print "please enter a number"
                for x in range(1, x, 1):
                    print(x)
                elif option == 2:
                    print "please enter a number"
                    for x in range(x, 1, 1):
                elif option == 0:
                    break
                else:
                    print("invalid command")
    elif option == 0:
        break

I got an error saying expected an indented block. I tried to check the indentation carefully, make sure all my indents use 4 spaces, etc. but I cannot find the problem.

What is wrong with the code, and how do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Zach
  • 359
  • 1
  • 3
  • 3
  • 3
    Can you edit your post and give us the raw error message w/ line numbers please :-) – Prydie Aug 01 '11 at 16:27
  • best practice would be to use tab and not count or use spaces to match tabs. I came to this with the same problem - the fix for me was that I had a conditional code block that was commented out except for the if/elif/else statement. Commenting in the code even when not True cleared the error message. – edwardmoradian Oct 31 '22 at 19:46

8 Answers8

18

Starting with elif option == 2:, you indented one time too many. In a decent text editor, you should be able to highlight these lines and press Shift+Tab to fix the issue.

Additionally, there is no statement after for x in range(x, 1, 1):. Insert an indented pass to do nothing in the for loop.

Also, in the first line, you wrote option == 1. == tests for equality, but you meant = ( a single equals sign), which assigns the right value to the left name, i.e.

option = 1
phihag
  • 278,196
  • 72
  • 453
  • 469
  • 2
    vim is a decent editor, but the keystrokes are `[Ctrl]-[<]` (in visual mode, add a motion beforehand in normal mode) – SingleNegationElimination Aug 01 '11 at 16:40
  • Thanks! This was very helpful. But now when I try to run my program it doesn't like **option == 1** saying **option** is undefined – Zach Aug 01 '11 at 17:01
  • @Zach You meant to write `option = 1` instead of `option == 1` in the first line, didn't you? Updated the answer. – phihag Aug 01 '11 at 17:03
  • +1 This works for me .. where I am getting error, above that line, I have just applied SHIFT+TAB and that works like charm (y) – Nirav Shah Mar 10 '15 at 21:02
9

In Python, indentation matters, e.g.:

if a==1:
    print("hey")

if a==2:
   print("bye")

print("all the best")

In this case, "all the best" will be printed if either of the two conditions executes, but if it would have been like this

if a==2:
   print("bye")
   print("all the best")

then "all the best" would be printed only if a==2.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bhawna
  • 91
  • 1
  • 1
6

Your for loop has no loop body:

elif option == 2:
    print "please enter a number"
    for x in range(x, 1, 1):
elif option == 0:

Actually, the whole if option == 1: block has indentation problems. elif option == 2: should be at the same level as the if statement.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
3

There are several issues:

  1. elif option == 2: and the subsequent elif-else should be aligned with the second if option == 1, not with the for.

  2. The for x in range(x, 1, 1): is missing a body.

  3. Since "option 1 (count)" requires a second input, you need to call input() for the second time. However, for sanity's sake I urge you to store the result in a second variable rather than repurposing option.

  4. The comparison in the first line of your code is probably meant to be an assignment.

You'll discover more issues once you're able to run your code (you'll need a couple more input() calls, one of the range() calls will need attention etc).

Lastly, please don't use the same variable as the loop variable and as part of the initial/terminal condition, as in:

            for x in range(1, x, 1):
                print x

It may work, but it is very confusing to read. Give the loop variable a different name:

            for i in range(1, x, 1):
                print i
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Your last for statement is missing a body.

Python expects an indented block to follow the line with the for, or to have content after the colon.

The first style is more common, so it says it expects some indented code to follow it. You have an elif at the same indent level.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
1

This one is wrong at least:

            for x in range(x, 1, 1):
        elif option == 0:
Achim
  • 15,415
  • 15
  • 80
  • 144
0

Use:

#option = 1
#while option != 0:

print ("MENU")
print("please make a selection")
print("1. count")
print("0. quit")
option = int(input("MAKE Your Selection  "))
if option == 1:
    print("1. count up")
    print("2. count down")
    print("0. go back")
    option = int(input("MAKE Your Selection  "))
    if option == 1:
        x = int(input("please enter a number   "))
        for x in range(1, x, 1):
            print (x)

    elif option == 2:
        x = int(input("please enter a number   "))
        for x in range(x, 0, -1):
            print (x)
    elif option == 0:
        print("hi")
    else:
        print("invalid command")
else:
    print ("H!111")

You can try this code! It works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.

src: ##

##https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator

Nilesh
  • 1