循环时Python缩进错误

Bre*_*sky 1 python syntax indentation syntax-error while-loop

while x <= 9:
    result = usertype()
    if result == 'Correct':
Run Code Online (Sandbox Code Playgroud)

我在"result = usertype()"的结果"t"上收到缩进错误.有人能解释一下吗?

*编辑我试过重写它,我检查确保所有的缩进确实是缩进而不是空格.现在变得非常沮丧和困惑.我认为它可能是导致问题的前一行中的某些内容.

*编辑2从命令提示符复制错误,因为IDLE对我不起作用:

  File "<stdin>", line 5
    result = usertype()
         ^
IndentationError: expected an indented block
>>>         if result == 'Correct':
  File "<stdin>", line 1
    if result == 'Correct':
    ^
IndentationError: unexpected indent
>>>             x = x + 1
  File "<stdin>", line 1
    x = x + 1
    ^
IndentationError: unexpected indent
>>>             y = y + 5
  File "<stdin>", line 1
    y = y + 5
    ^
IndentationError: unexpected indent
>>>         else:
  File "<stdin>", line 1
    else:
    ^
IndentationError: unexpected indent
>>>             x = x + 1
  File "<stdin>", line 1
    x = x + 1
    ^
IndentationError: unexpected indent
>>>             y = y - a2
  File "<stdin>", line 1
    y = y - a2
    ^
IndentationError: unexpected indent
>>>     return y
  File "<stdin>", line 1
    return y
    ^
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)

解决了! 我不知道怎么做,但是我将我的代码粘贴到IDLE中,所有区域都没有修改,然后再次列表.我在其他地方读到了这样做,所以问题解决了!

这是整个计划:

import random

def usertype(raw_input):
    randletter = random.choice('qwer')
    print randletter
    userinput = raw_input('')
    if userinput == randletter:
        return 'Correct'
    else:
        return 'Incorrect'

def usertypetest(raw_input):
    x=0
    y=0
    while x <= 9:
        result = usertype()
        if result == 'Correct':
            x = x + 1
            y = y + 5
        else:
            x = x + 1
            y = y - a2
    return y

print usertypetest(raw_input)
Run Code Online (Sandbox Code Playgroud)

Sim*_*ser 5

发布的代码没有问题.但是,您的文件中可能有混合选项卡和空格,这会导致Python对缩进感到困惑.检查文本编辑器设置以查看是否可以突出显示这些字符.