使用随机函数和列表在python中进行多项选择测验

时间:2017-05-30 13:16:37

标签: python list loops random multiple-choice

我正在尝试使用python进行多项选择测验。起初看起来似乎很简单,但现在我正在试图弄清楚如何做某些事情。

我使用两个列表;一个用于问题,一个用于答案。我想这样做是为了从问题列表中选择一个随机问题,以及答案列表中的2个随机项(错误答案),最后是正确答案(与随机选择的问题具有相同的索引)。 我已经选择了一个随机问题和两个随机错误答案

  • 我的第一个问题是让程序显示正确的答案。
  • 第二个问题是如何检查用户是否输入了正确的答案。

我很感激我的代码的任何反馈。我对此非常陌生,所以请稍等一点! 我希望我的东西是可读的(对不起,它有点长)

提前致谢

import random

print ("Welcome to your giongo quiz\n")
x=0
while True:
    def begin(): # would you like to begin? yes/no... leaves if no
        wanna_begin = input("Would you like to begin?: ").lower()
        if wanna_begin == ("yes"):
            print ("Ok let's go!\n")
            get_username()#gets the user name
        elif wanna_begin != ("no") and wanna_begin != ("yes"):
            print ("I don't understand, please enter yes or no:")
            return begin()
        elif wanna_begin == ("no"):
            print ("Ok seeya!")
    break

def get_username(): #get's username, checks whether it's the right length, says hello 
    username = input("Please choose a username (1-10 caracters):")
    if len(username)>10 or len(username)<1:
        print("Username too long or too short, try again")
        return get_username()
    else:
        print ("Hello", username, ", let's get started\n\n") 
        return randomq(questions)

##########

questions = ["waku waku", "goro goro", "kushu", "bukubuku", "wai-wai", "poro", "kipashi", "juru", "pero"]

answers = ["excited", "cat purring", "sneezing", "bubbling", "children playing", "teardrops falling", "crunch", "slurp", "licking"]

score = 0

if len(questions) > 0: #I put this here because if i left it in ONly inside the function, it didnt work...
        random_item = random.randint(0, len(questions)-1)
        asked_question = questions.pop(random_item)

###########

def randomq(questions):
    if len(questions) > 0:
        random_item = random.randint(0, len(questions)-1)
        asked_question = questions.pop(random_item)
        print ("what does this onomatopea correspond to?\n\n%s" % asked_question, ":\n" )
        return choices(answers, random_item)
    else:
        print("You have answered all the questions")
        #return final_score

def choices(answers, random_item):
    random_item = random.randint(0, len(questions)-1)
    a1 = answers.pop(random_item)
    a2 = answers.pop(random_item)
    possible_ans = [a1, a2, asked_question"""must find way for this to be right answer"""]
    random.shuffle(possible_ans)
    n = 1
    for i in possible_ans:
        print (n, "-", i) 
        n +=1
    choice = input("Enter your choice :")
    """return check_answer(asked_question, choice)"""

    a = questions.index(asked_question)
    b = answers.index(choice)
    if a == b:
        return True
    return False

begin()

2 个答案:

答案 0 :(得分:0)

您可以使用其他列表(例如correct_answer等)来记录每个问题的正确答案。然后使用

而不是使用a1 = answers.pop(random_item)选择错误的答案
while True:
    if answer[random.randint(0, len(answers)-1)] != correct_answer[question]:
        break
a1 = answers.pop(id)

避免选择正确的答案作为错误答案。

修改

由于您的正确答案已经在answers中,因此在选择不正确的答案时不应弹出项目,否则会破坏索引。

您可以选择两个错误答案和正确答案。

correct = question_index

while True:
    wrong1 = random.randint(0, len(answers)-1)
    if wrong1 != correct:
        break

while True:
    wrong2 = random.randint(0, len(answers)-1)
    if wrong1 != wrong2 and wrong1 != correct:
        break

答案 1 :(得分:0)

您可以使用词典以更简单的方式构建数据。每个项目都是一对键和一个值,有关详细信息,请参阅this。 例如,您的数据将如下所示(每个问题都与答案相关联):

data = {'question1': 'answer1', 'question2': 'answer2'}

然后我们可以循环浏览这个词典,在我们随机选择一个问题后打印答案。这是一些帮助的sudo代码:

# Choose a random question

# Print all the answers in the dictionary
for key, value in data.iteritems(): #its data.items() in Python 3.x
     print value

# If the choice matches the answer
pop the question from the dictionary and do whatever you want to do

# Else, ask again

有关迭代字典的更多信息,请参阅this

相关问题