Making a quiz, how do I store the questions?
我想做一个有多种选择(4种选择)的测验游戏。到目前为止,我做了一个简单的测验,只包含一个问题。我不能用一种好的方法来概括问题。
计划是将我的测验扩展到至少500个问题,然后从问题库中随机选择一个问题。我应该如何构建它?
这就是我在一个问题游戏中所得到的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | def welcome(): #Introduction print"Welcome to the quiz!" print"" print"" def question(): # Question function quest = { 'id' : 0, 'question' :"What is the capital of Belgium?" , 'a' :"Vienna" , 'b' :"Berlin" , 'c' :"Brussels" , 'd' :"Prague" , 'answer' : 'c'} print quest['question'] print"" print"A:", quest['a'], print"B:", quest['b'], print"C:", quest['c'], print"D:", quest['d'] guess=raw_input("Your guess:") if guess == quest['answer']: print"" print"Correct!!!" else: print"" print"Sorry, that was just plain wrong!" welcome() question() |
您可以创建一个包含所有这些数据的字典列表。所以你可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | quiz_data = [ { "question":"What year is it", "choices": {"a":"2009","b":"2016","c":"2010 <hr><P>我觉得测验太复杂了。这个代码更容易阅读,IMO也更短。</P>[cc lang="python"] point = 0 print("The answer have to be in all small letters") def question(question,rightAnswer,rightAnswer2): global point answer = input(question) if answer == (rightAnswer): point = point + 1 print("Right") elif answer == (rightAnswer2): point = point + 1 print("Right") else: print("Wrong") if point == 1: print("You have",point,"point") else: # grammar print("You have",point,"points") return question("What is 1+1?","2","2") #question(<"question">,answer,otheranswer) question("What is the meaning of life","42","idk") |
我尝试导入随机函数,然后生成一个介于1和(问题数)之间的随机数。假设你有10个问题,你可以这样打字;
1 2 | import random (Number) = (random.randint(1,10)) |
然后,您将所有问题逐个添加到if语句下面,如图所示;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | if (Number) == (1): print ("What's 1 + 1?") (Answer) = input() if (Answer) == ('2'): print ('Correct!') else: print ('Wrong!') elif (Number) == (2): print ("What's 1 + 2?") (Answer) = input() if (Answer) == ('4'): print ('Correct!') else: print ('Wrong!') |
等等。
如果要使其重复,并提出多个问题,请从开始编码;
1 | while (1) == (1): |
然后你有一个有效的测验项目。希望有人能帮上忙。
我会(根据你的代码):
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #!/usr/bin/python from random import randint def welcome(): #Introduction print"Welcome to the quiz!" print"" print"" def question(): # Question function question_pool = [] question_pool.append({'question' :"What is the capital of Belgium?" , 'a' :"Vienna" , 'b' :"Berlin" , 'c' :"Brussels" , 'd' :"Prague" , 'answer' : 'c'}) question_pool.append({'question' :"Does Stackoverflow help?" , 'a' :"Yes" , 'b' :"A lot" , 'c' :"Of course" , 'd' :"Hell yeah" , 'answer' : 'd'}) random_idx = randint(0, len(question_pool) - 1) print question_pool[random_idx]['question'] print"" print"A:", question_pool[random_idx]['a'], print"B:", question_pool[random_idx]['b'], print"C:", question_pool[random_idx]['c'], print"D:", question_pool[random_idx]['d'] guess=raw_input("Your guess:") guess = guess.lower() if guess == question_pool[random_idx]['answer']: print"" print"Correct!!!" else: print"" print"Sorry, that was just plain wrong!" welcome() question() |
下一步是验证输入,例如检查用户是否键入了字母A、B、C或D。
有帮助的问题:
我确信柏林不是比利时的首都
您应该创建一个txt文件并将问题放入该文件。之后,您可以使用
基本上,您将把您的问题写在一个txt文件中。然后阅读这些行并用
制作另一个txt文件以获取答案,当用户回答问题时检查该文件。
Store it as a JSON array
1 2 3 4 5 6 7 8 9 | [{ "id": 0, "question":"What is the capital of Belgium?", "a":"Vienna", "b":"Berlin", "c":"Brussels", "d":"Prague", "answer":"c" }] |
并使用