Python:如何为参与者提供答案的可能性

时间:2015-12-25 23:02:48

标签: python while-loop psychopy

我正在做一个实验,参与者必须有可能在他给出错误答案时纠正自己。

目标是当给出正确答案时,实验将继续进行下一次试验。当给出错误答案时,您将获得另一次机会。

目前,实验在第一次试验后崩溃,并且总是等待第二次机会回答(即使给出了正确答案)。

2 个答案:

答案 0 :(得分:0)

您可能将Answerrunning = False放在了错误的地方。也许您需要将break放在每个分支的末尾。请详细说明你想做什么,我不明白。

如果你说你需要计算尝试次数,那么我猜你的代码中应该有number_of_tries = 0number_of_tries += 1

答案 1 :(得分:0)

好吧,看起来有一些代码,但不再是这样,我不知道这有什么问题,但根据你的描述,你可能需要这样的东西:

def ask_user(question,answer):
    """ask the user some question and return if the expected answer was given """
    return answer == input(question)

def trial(question,answer,chances=3):
    """repeatedly ask the user a question by a number of chances and return True
       if the expected answer is given, otherwise return False"""
    for i in reversed( range(chances) ):
        if ask_user(question,answer):
            return True
        else:
            print("wrong answer you have",i,"more chances")
    return False

def test(trials,chances=3):
    """given a list of tuples of the form (question,answer) ask the user for
       an answer to each question given him/her a number of chances in each 
       one and return True if all of them are answered correctly otherwise 
       return False at the first failure """
    print("Welcome to this test, please answer the next few questions:")
    for i,(q,a) in enumerate(trials,1):
        print("questions #",i)
        if not trial(q,a,chances):
            return False
    return True

def my_test():
    my_trials=[("1+2= ","3"),
           ("translate 'home' to Spanish: ","casa"),
           ("what is the Answer to the Meaning of Life, the Universe, and Everything Else: ","42")
           ]
    print("you pass the test" if test(my_trials) else "You fail this test")

这是相当通用的,所以你可以通过定义一个像my_test