首页 > 解决方案 > 我在 python3 中得到 TabError 但在 python2 中没有

问题描述

它在 python2 中工作得很好,但在 python3 中出现此错误-语句“b = b + a”上的“TabError:缩进中制表符和空格的不一致使用”。已经尝试删除空格并检查缩进是否正确。

def dice():
b=0
global a
a=random.randint(1,6)
    b=b+a
if(b>22):
    b=b-22 #Since 22 blocks in our board, one lap completion will result in 
a decrease of 22
#print b
return b

标签: pythonpython-3.xpython-2.7

解决方案


在 if 语句和函数定义中,冒号语法后都需要制表符。尝试这个:

def dice():
    b=0
    global a
    a=random.randint(1,6)
    b=b+a
    if(b>22):
        b=b-22 #Since 22 blocks in our board, one lap completion will result in a decrease of 22
    #print b
    return b

推荐阅读