Python “expected an indented block”

2020-02-04 07:00发布

Let me start off by saying that I am COMPLETELY new to programming. I have just recently picked up Python and it has consistently kicked me in the head with one recurring error -- "expected an indented block" Now, I know there are several other threads addressing this problem and I have looked over a good number of them, however, even checking my indentation has not given me better results. I have replaced all of my indents with 4 spaces and even rewritten the code several times. I'll post this counter assignment I got as an example.

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

标签: python
8条回答
欢心
2楼-- · 2020-02-04 07:10
#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.
查看更多
神经病院院长
3楼-- · 2020-02-04 07:12

in python .....intendation 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" will be printed only if a==2

查看更多
beautiful°
4楼-- · 2020-02-04 07:15

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.

查看更多
地球回转人心会变
5楼-- · 2020-02-04 07:17

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
查看更多
女痞
6楼-- · 2020-02-04 07:27

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

查看更多
Emotional °昔
7楼-- · 2020-02-04 07:29

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.

查看更多
登录 后发表回答