如何在python中随机分配变量?

时间:2018-04-05 13:50:13

标签: python python-3.x pygame

您好我正在尝试在pygame中进行简单的多项选择测验。这是我编写的代码,它按预期工作。

from random import choice
qUsed=[]
qEasy = [
    {
        "question": "What year is it",
        "choices": {"a": "2009", "b": "2016", "c": "2010"},
        "answer": "b"
    },
    {
        "question": "Another Question",
        "choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
        "answer": "a"
    },
    {
        "question": "Another Question",
        "choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
        "answer": "a"
    },
    {
        "question": "Another Question",
        "choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
        "answer": "a"
    },
    {
        "question": "Another Question",
        "choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"},
        "answer": "a"
    }
]

def Quiz(qLevel):
    global qUsed
    if qLevel == []: # if qLevel becomes empty
        qLevel = qUsed # dump everything from qUsed to qLevel
        qUsed = [] # reset qUsed to an empty list
    x = choice(qLevel) # assign x to a random question in qLevel
    print(x.get('question')) # print the question
    answer = input(x.get('choices')).lower() # print the choices

    if answer == x.get('answer'): # if answer is correct
        print("You got it")
    else:
        print("Wrong")
    qLevel.remove(x) # remove used question from list
    qUsed.append(x) # add it to an empty unused list (qUsed)

Quiz(qEasy)
Quiz(qEasy)

当我写一个pygame脚本来打开一个窗口,问题作为标题并且三个可能的答案随机地在三个黑色矩形上进行时,问题就出现了。我想要做的是从列表中随机选择一个问题,在屏幕上显示问题"在此处插入问题"并随机分配三个矩形的答案。如果要按右方矩形,则矩形会将颜色更改为绿色,否则会变为红色。但我无法弄明白该怎么做。

def Screen():
mouse.set_visible(1)
clock = time.Clock()
# The button is just a rect.
A1 = Rect(30, 100, 175, 250)
A2 = Rect(230, 100, 175, 250)
A3 = Rect(430, 100, 175, 250)

while True:

    screen.fill(WHITE)
    draw.rect(screen, BLACK, A1)
    draw.rect(screen, BLACK, A2)
    draw.rect(screen, BLACK, A3)

    Question_surf = FONT.render(("Insert Question Here"), True, BLACK)
    A1_surf = FONT.render(("Answer 1"), True, WHITE)
    A2_surf = FONT.render(("Answer 2"), True, WHITE)
    A3_surf = FONT.render(("Answer 3"), True, WHITE)

    A1_rect = A1_surf.get_rect(center=(120, 225))
    A2_rect = A2_surf.get_rect(center=(320,225))
    A3_rect = A3_surf.get_rect(center=(520,225))
    Question_rect = Question_surf.get_rect(center=(320, 50))

    screen.blit(A1_surf, A1_rect)
    screen.blit(A2_surf, A2_rect)
    screen.blit(A3_surf, A3_rect)
    screen.blit(Question_surf, Question_rect)

    for e in event.get():
        if e.type == QUIT:
            exit("Exit")
        # This block is executed once for each MOUSEBUTTONDOWN event.
        elif e.type == MOUSEBUTTONDOWN:
            # 1 is the left mouse button, 2 is middle, 3 is right.
            if e.button == 1:
                # `event.pos` is the mouse position.
                if A1.collidepoint(e.pos):
                    pass
                elif A2.collidepoint(e.pos):
                    pass
                elif A3.collidepoint(e.pos):
                    pass

    display.update()

    clock.tick(60)

if __name__ == "__main__":
   Screen()

2 个答案:

答案 0 :(得分:1)

要循环查看我定义index变量的问题,并将当前的问题选择答案dict分配给变量(我在这里称之为question)。

questions = qEasy
random.shuffle(questions)
index = 0
question = questions[index]

当用户切换到下一个question时增加索引(我在事件循环中这样做)。

index += 1
if index >= len(questions):
    # Reset everything or show a game over screen.
    index = 0
question = questions[index]

将问题文本和选项传递给font.render(只有在需要更新时才能呈现文本一次。)

Question_surf = FONT.render(question["question"], True, BLACK)
A1_surf = FONT.render(question["choices"]["a"], True, WHITE)
# etc.

当用户点击按钮时,检查相应的答案是否正确,然后更改按钮/ rect的颜色。

if A1.collidepoint(e.pos):
    if question["answer"] == "a":
        A1_color = GREEN
    else:
        A1_color = RED

将当前的矩形颜色传递给pygame.draw.rect

pygame.draw.rect(screen, A1_color, A1)

当用户切换到下一个问题时,请重置按钮颜色。

答案 1 :(得分:-1)

列表是一种能够获得随机答案的非常好的方法。

你首先要进入ranged for循环:

data tmp2;
set tmp;
length s $10 ;
s ='x';
t = cats(s,'y');
s = cats(s,'y');
run;

data tmp3;
set tmp2;
a = 0;
do i=1 to 4;
    a = a+i;
    s=cats(s,'y');
end;
run;

然后,您可以在需要绘制

时索引新列表

for i in range(3): myList.append(answers['Your question'][random.randint(0, 3)])

稍后您可能想要添加一些重复检测代码,但我希望这会有所帮助。