simplegui缩进错误:SyntaxError:错误输入('')

时间:2015-07-10 09:18:52

标签: python codeskulptor

我在Coursera上做了一个迷你项目,我可以运行代码的大部分内容。但是,关于游戏match or not检查的关键部分存在错误。

# implementation of card game - Memory

import simplegui
import random

# helper function to initialize globals
def new_game():
    global turns, state, pairs, cards
    turns = 0
    state = 0
    pairs = []
    cards = range(9) * 2
    random.shuffle(cards)

# define event handlers
def mouseclick(pos):
    # add game state logic here
    global turns, state, pairs
    pointed = pos[0] // 50
    if  pointed in pairs:
        pass
    else:
        if state == 0:
            state = 1
            pairs.append(pointed)
        elif state == 1:
            state = 2
            turns += 1
            label.set_text('Turns =' + str(turns))
            pairs.append(pointed)
#                if cards[pairs[-2]] == cards[[pairs[-1]]:
#                    flag = True
#                else:
#                    flag = False
        else:
            state = 1
            if flag == False:
                del pairs[-2:]
            pairs.append(pointed)

# cards are logically 50x100 pixels in size    
def draw(canvas):
    for n in range(1, 16):
        canvas.draw_line((n * 50, 0), (n * 50, 100), 1, 'Green')

    for n in pairs:
        canvas.draw_line((n * 50 + 25, 0), (n * 50 + 25, 100), 50, 'White')

    for n in pairs:
        canvas.draw_text(str(cards[n]), (n * 50 + 15, 65), 50, 'Black')


# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.set_canvas_background('Red')
frame.add_button("Reset", new_game)
label = frame.add_label("Turns = 0")

# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)

# get things rolling
new_game()
frame.start()

# Always remember to review the grading rubric

我评论了第31至34行,这是我遇到问题的部分。控制台一直告诉我Line 31: SyntaxError: bad input (' '),但我认为缩进是正确的。

请帮我弄清楚为什么这是一个糟糕的输入',非常感谢!

更新

感谢Russell的帮助,此功能现在可以使用。

# define event handlers
def mouseclick(pos):
    # add game state logic here
    global turns, state, pairs, flag
    pointed = pos[0] // 50
    if pointed in pairs:
        pass
    else:
        if state == 0:
            state = 1
            pairs.append(pointed)
        elif state == 1:
            state = 2
            turns += 1
            label.set_text('Turns =' + str(turns))
            pairs.append(pointed)
            if cards[pairs[-2]] == cards[pairs[-1]]:
                flag = True
            else:
                flag = False
        else:
            state = 1
            if flag == False:
                del pairs[-2:]
            pairs.append(pointed)

1 个答案:

答案 0 :(得分:2)

您的if语句缩进太多了。

elif state == 1:
    state = 2
    turns += 1
    label.set_text('Turns =' + str(turns))
    pairs.append(pointed)
    if cards[pairs[-2]] == cards[pairs[-1]]:
        flag = True
    else:
        flag = False
else:
    state = 1
    if flag == False:
        del pairs[-2:]
    pairs.append(pointed)