# Oct 8, 2015
# TriviaGame
import Question
Questions = ["How many licks does it take to get to the center of a Tootsie Pop™ ?",
"What does the scouter say about his power level", ]
answer1 = ["10", "Over 9000"]
answer2 = ["20", "1000"]
answer3 = ["30", "10000"]
answer4 = ["The world may never know", "5000"]
correct = [4, 1]
questionList = list()
player1 = 0
player2 = 0
x = 0
turns = 2
while x < turns:
q = Question.Question(Questions[x], answer1[x], answer2[x], answer3[x], answer4[x], correct[x])
questionList.append(q)
x += 1
turn = 0
while turn < turns:
if turn % 2 != 0:
print("Player 2 Answer ")
questionList[turn].ask()
response = int(input("Enter a number"))
q.correct(response, player2)
if q.correct(response, player2):
player2 += 1
else:
questionList[turn].ask()
response = int(input("Enter a number"))
q.correct(response, player1)
if q.correct(response, player1):
player1 += 1
turn += 1
if q.correct(response,player1):
print("Nice job. You got it right")
else:
print("Your wrong")
上面的代码应该询问用户一个问题,并根据哪个答案是正确的,将它们放入一个整数。答案设置为
correct = [4, 1]
问题是答案总是1,即使第一个答案应该是4.
编辑:问题模块
class Question:
def __init__(self, question, answer_1, answer_2, answer_3, answer_4, correct_answer):
self.__question = question
self.__answer_1 = answer_1
self.__answer_2 = answer_2
self.__answer_3 = answer_3
self.__answer_4 = answer_4
self.__correct_answer = correct_answer
def ask(self):
print(self.__question)
print("1. " + self.__answer_1)
print("2. " + self.__answer_2)
print("3. " + self.__answer_3)
print("4. " + self.__answer_4)
def correct(self, response, player1):
if response == self.__correct_answer:
return True
else:
return False
答案 0 :(得分:1)
您的代码正在询问Question
中的questionList[turn]
,但您正在针对名为Question
的{{1}}检查答案。从您创建q
的早期循环中遗留下来,它将始终保留您创建的最后一个问题。
要使代码正常工作,您需要访问相同的问题以进行询问和检查。将最后一个循环中questionList
的引用替换为q
或将questionList[turn]
重新绑定到某个地方的当前问题。