首页 > 解决方案 > “预期有缩进块”错误,但有一个缩进块

问题描述

我收到语法“预期有缩进块”错误,但那里肯定有一个缩进块。我已经检查并重写了一百万次。

#Game loop:
while True: #The error is occurring here
    load()
    #Main game loop:
    while game_over == False:
            load_stage()
    #Win screen and restart option:
    print('\n**********************************************')
    print ('Congratulations! You Won!')
    answer = input('Would you like to restart?\nIf so type \'restart\'.')
    if answer == 'restart':
        restart()
        save()
    else:
        pass

标签: python-3.x

解决方案


尝试:

#Game loop:
while True: #The error is occurring here
    load()
    #Main game loop:
    while game_over == False:
        load_stage()
    #Win screen and restart option:
    print('\n**********************************************')
    print ('Congratulations! You Won!')
    answer = input('Would you like to restart?\nIf so type \'restart\'.')
    if answer == 'restart':
        restart()
        save()
   

错误出现是由于缩进不一致,这意味着您的程序没有一致的缩进方案。一般来说,建议使用 4 个空格(其中空格 = 单个空格键)来表示缩进块。

如果您的程序交替使用 Tab 和空格进行缩进,也会出现不一致的情况。


推荐阅读