Python 做一个测验,我如何存储问题?
我想做一个多选(4个选项)的问答游戏。到目前为止,我做了一个简单的测验,只包含一个问题。我想不出一个好办法来索引这些问题 计划是用至少500个问题扩展我的测验,并从问题库中随机挑选一个问题。我应该如何构建它 这就是我在一个问题游戏中得到的结果:Python 做一个测验,我如何存储问题?,python,Python,我想做一个多选(4个选项)的问答游戏。到目前为止,我做了一个简单的测验,只包含一个问题。我想不出一个好办法来索引这些问题 计划是用至少500个问题扩展我的测验,并从问题库中随机挑选一个问题。我应该如何构建它 这就是我在一个问题游戏中得到的结果: def welcome(): #Introduction print "Welcome to the quiz!" print " " print " " def question(): # Question functi
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()
您可以创建包含所有这些数据的词典列表。所以你可以这样做:
quiz_data = [
{
"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"
}
]
然后使用random.choice
选择数据结构的随机索引
import random
q = random.choice(quiz_data)
print(q.get('question'))
answer = input(q.get('choices')).lower()
if answer == q.get('answer'):
print("You got it")
else:
print("Wrong")
您可以创建包含所有这些数据的词典列表。所以你可以这样做:
quiz_data = [
{
"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"
}
]
然后使用random.choice
选择数据结构的随机索引
import random
q = random.choice(quiz_data)
print(q.get('question'))
answer = input(q.get('choices')).lower()
if answer == q.get('answer'):
print("You got it")
else:
print("Wrong")
将其存储为数组
并使用加载。将其存储为数组
然后使用加载。您应该创建一个txt文件,并将问题放入该文件中。然后,您可以读取该文件的行,并使用
random.choice()
方法随机选择一行(这里的问题是行)
基本上,您将把问题写在txt文件中。然后用random.choice()
阅读并打印一行(问题)
为答案创建另一个txt文件,当用户回答问题时检查该文件。您应该创建一个txt文件,并将问题放入该文件中。然后,您可以读取该文件的行,并使用
random.choice()
方法随机选择一行(这里的问题是行)
基本上,您将把问题写在txt文件中。然后用random.choice()
阅读并打印一行(问题)
为答案制作另一个txt文件,当用户回答问题时检查该文件。我会(基于您的代码)):
id
属性,我看不到
现在使用它的原因#!/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
有帮助的问题:
我很确定柏林不是比利时的首都:)我会(根据您的代码)):
id
属性,我看不到
现在使用它的原因#!/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
有帮助的问题:
我很确定柏林不是比利时的首都:)我会尝试导入随机函数,然后生成一个介于1和(问题数量)之间的随机数。假设你有10个问题,你可以这样输入
import random
(Number) = (random.randint(1,10))
然后,将所有问题逐一添加到if语句下,如图所示
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!')
等等
如果你想让它重复,并问多个问题,开始编码
while (1) == (1):
然后你就有了一个有效的测试程序。我希望有人会觉得这很有帮助。我会尝试导入随机函数,然后生成一个介于1和(问题数量)之间的随机数。假设你有10个问题,你可以这样输入
import random
(Number) = (random.randint(1,10))
然后,将所有问题逐一添加到if语句下,如图所示
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!')
等等
如果你想让它重复,并问多个问题,开始编码
while (1) == (1):
然后你就有了一个有效的测试程序。我希望有人觉得这很有帮助。我觉得这个测验太复杂了。 这个代码也更容易阅读,而且更短
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")
点=0
打印(“答案必须用小写字母填写”)
def问题(问题,正确答案,正确答案2):
全局点
回答=输入(问题)
如果答案==(右答案):
点=点+1
打印(“右”)
elif答案==(右答案2):
点=点+1
打印(“右”)
其他:
打印(“错误”)
如果点==1:
打印(“你有”,点,“点”)
其他:#语法
打印(“您有”,点,“点”)
返回
问题(“1+1是什么?”,“2”,“2”)#问题(,答案,其他答案)
问题(“生命的意义是什么”、“42”、“idk”)
我认为那个测验太复杂了。
这个代码也更容易阅读,而且更短
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")
点=0
打印(“答案必须用小写字母填写”)
def问题(问题,正确答案,正确答案2):
全局点
回答=输入(问题)
如果答案==(右答案):
点=点+1
打印(“右”)
elif答案==(右答案2):
点=点+1
打印(“右”)
其他:
打印(“错误”)
如果点==1:
打印(“你有”,点,“点”)
其他:#语法
打印(“您有”,点,“点”)
返回
问题(“1+1是什么?”,“2”,“2”)#问题(,答案,其他答案)
问题(“生命的意义是什么