我正在尝试制作一个有多种选择的测验游戏(4种选择)。到目前为止,我做了一个只包含一个问题的简单测验。我无法绕过一个很好的方法来索引问题。
计划是用至少500个问题扩展我的测验,然后从问题池中随机选择一个问题。我该如何构建呢?
这是我在一个问题游戏中到目前为止所得到的:
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()
答案 0 :(得分:3)
您可以创建一个列出所有这些数据的词典列表。所以你可以这样做:
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")
答案 1 :(得分:0)
将其存储为JSON数组
[{
"id": 0,
"question": "What is the capital of Belgium?",
"a": "Vienna",
"b": "Berlin",
"c": "Brussels",
"d": "Prague",
"answer": "c"
}]
并使用json.load
加载它。
答案 2 :(得分:0)
您应该创建一个txt文件并将问题放入该文件中。之后你可以用random.choice()
方法随机读取该文件的行并选择一行(行是这里的问题)。
基本上,您将在txt文件中写下您的问题。然后阅读这些行并用random.choice()
打印一行(问题)。
制作另一个txt文件以获得答案,在用户回答问题时检查该文件。
答案 3 :(得分:0)
我会(基于您的代码):
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.
有帮助的问题:
答案 4 :(得分:0)
我会尝试导入随机函数,然后生成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):
然后你有一个工作的测验程序。我希望有人发现这有用。
答案 5 :(得分:0)
我认为测验过于复杂。 这段代码更容易阅读和更短的imo。
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")