连续的随机问题?
问题描述:
我想要我现在必须通过一个无限的问题列表,或直到有人得到一个答案错误的代码。我目前使用的是连续的随机问题?
random.shuffle(questions)
for question in questions:
question.ask()
要求列表中的每一个问题都需要一次。
如何在用户输入错误答案之前持续询问?这是我目前的代码:
class Question(object):
def __init__(self, question, answer):
self.question = question
self.answer = answer
def ask(self):
response = input(self.question)
if response == self.answer:
print "CORRECT"
else:
print "wrong"
questions = [
Question("0", 0),
Question("π/6", 30),
Question("π/3", 60),
Question("π/4", 45),
Question("π/2", 90),
Question("2π/3", 120),
Question("3π/4", 135),
Question("5π/6", 150),
Question("π", 180),
Question("7π/6", 210),
Question("4π/3", 240),
Question("5π/4", 225),
Question("3π/2", 270),
Question("5π/3", 300),
Question("7π/4", 315),
Question("11π/6", 330),
Question("2π",360),
]
此外,如果你能告诉我如何为每个问题添加一个分数正确的,将不胜感激。我试图这样做,但我已经有一段程序每5秒钟从一个全局评分变量中扣除1。我想继续编辑相同的变量,但它会给出错误。
答
这可能是值得一试给问()返回值。如果答案正确,则为真;如果答案不正确,则为假。这可能是这样的:
(!你会首先必须创建一个分数变量)
for q in questions:
if q.ask() is True:
score += 1
else:
break #Breaks out of the while loop
Eitherway,你必须:
def ask(self):
response = input(self.question)
if response == self.answer:
print "CORRECT"
return True
else:
print "wrong"
return False
然后,你可以通过这样的问题迭代让你的答案为了不把字符串与整数(它将永远不会相同)进行比较,所以问题应该如下所示:
questions = [
Question("0", "0"),
Question("π/6", "30"),
Question("π/3", "60"),
Question("π/4", "45"),
...
我希望我能帮助你!
答
你可以遍历列表while循环这样的事情可能
score = 0
currIndex = 0
#ask a question to start off
q1 = questions[currIndex]
#get the answer
answer = q1.ask()
while(answer == q1.answer):
#ask the question at this index
score+=1
q1=questions[currIndex]
answer = q1.ask()
currIndex+=1
#reset the loop?
if currIndex == len(questions)-1:
currIndex = 0
没有测试它尚未但这应该工作?它会一直持续下去,直到他们得到答案错误,否则无限。 编辑:哎呦没读过完全,我会做问返回正确或错误,则循环改变
while (answer == "CORRECT"):
这真的是你的缩进吗? – Li357
[Python 3.4.3中的循环]的可能重复或任何数量的“不断询问用户,直到___”的问题[1](http://stackoverflow.com/questions/20337489/python-how-to-keep-repeating-a-program-until-a-specific-input-is-obtained),[2](http:/ /stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response),[3](http://stackoverflow.com/questions/8114355/loop-直到特定用户输入),[4](http://stackoverflow.com/questions/12556907/continually-prompting-user-for-input-in-python)等 – TessellatingHeckler
哦,我的坏,它似乎已经搞乱了,当我复制,我会修复 – Warstolrem