2017-09-02 71 views
1

如果问题在一轮中的数量不是10,因为上一轮的错误答案被添加到问题中,我有一堆措辞数字作为问题,需要将它们添加到另一轮中第二轮。我可以将问题添加到第2轮,但是如何将问题与单独列表中的答案进行匹配?或者我可以把它们放在同一个列表中吗?从单独的列表中匹配问题和答案

extraquestions=[] 
extraanswers=[] 

q30='two thousand and two' 
a30='2002' 
extraquestions.append(q30) 
extraanswers.append(a30) 

x = len(round2questions) 
while x != 10: 
    round2questions.append(extraquestions[random.randint(0,18)]) 

我有19个多问题

+0

最好使用'while x <10'。这样,如果'x'恰好为11,由于附加到列表中的项目数量,程序不会耗尽内存。还记得增加'x' – smac89

回答

1

这将是每一个问题更好地组和一个元组回答,于是,例如,你将有extraquestions = [('three',3),('twelve',12),...,('two thousand and two',2002)]。然后,您可以将(question,answer)配对添加到您的round2questions列表中。否则,如果您更愿意保留两个单独的列表,我想您可以将随机索引保存到一个变量中,以便同时获得问题和答案。顺便说一句,你不应该在循环条件中使用变量x,因为它不会“刷新”每个append

while len(round2questions) != 10: 
    i = random.randint(0,18) 
    round2questions.append(extraquestions[i]) 
    round2answers.append(extraanswers[i]) 
+0

非常感谢你,我想我不能交任务。 – Jack

+0

你有什么想法如何回答最多10个问题的问题。我可以问问题只需循环它。 – Jack

+0

R = 0 RC = 0 RW = 0 r2q = round2questions [R] R2A = round2answers [R] 打印(r2q) R2P =输入( '转换此成数') 如果R2P = = R2A: R + = 1 RC + = 1 N + = 1张 打印( '下一个问题') elif的R2P = R2A: R + = 1 RW + = 1 N + = 1张 打印( '下一个问题') – Jack

1

我不喜欢while循环我自己,没有什么东西是一个while循环可以做一个for循环不能做的更好。

您可以通过更改while循环节省一些头痛

for x in range(9): 

让我解释一下为什么这是更好的。 while循环对你的例子很好,但是当你开始深入细节的时候,while循环有问题,它们可能永远不会结束。使用for循环会给出一个特定的中断条件,在某些时候总会得到满足,除非您的中断点是len(someList)并且您将增加该列表。 x将在循环结束时以0递增1,当x达到10时循环结束。

如果使用

while len(someList) != 10 

如果列表的长度是10从来没有什么?那么循环将永远不会结束。 for循环以增量方式工作,while循环以布尔逻辑工作。大多数情况下,循环说明:

while True: 
    if "some variable" == "some condition": 
     break 
    else: 
     "perform a task" 
+0

的评论的原因for循环也不能解决问题,因为您没有强制执行硬迭代计数,其中该值应该由列表中的项目数决定。你可以通过使用'for x in range(10-len(someList))'来解决这个问题,这样for循环仍然受列表中项目数量的限制 – smac89

0

嗯。这是我我把你的问题一起:
1)你有几个措辞数字和答案:

round1questions=["two thousand and two", "one thousand and one"] # and more 
round2answers=["2002", "1001"] # and more 

2)从我对你的问题的理解,在每一轮,十个问题被问和TEN答案被采取...
3)如果任何问题得到了错误的答案,在另一回合中再次询问错误回答的问题。

因此,这里是我的问题的解决方案:

questions=["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"] 
answers=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] 
ans_correct=[None, None, None, None, None, None, None, None, None, None] 

#You can use this method to add "None" to the ans_correct list if you have many questions 
#and you're too lazy to write so many "None"s. 
''' 
ans_correct=[] 
for each in questions: 
    ans_correct.append(None) 
''' 

while (False in ans_correct) or (None in ans_correct): 
    for each in questions: 
     if ans_correct[questions.index(each)]!=True: 
      ans=str(input("What is the numerical form of %s?" %(each))) 
      if ans==answers[questions.index(each)]: 
       print("Correct!") 
       ans_correct[questions.index(each)]=True 
      else: 
       print("Wrong!") 
       ans_correct[questions.index(each)]=False 

基本上,上面的代码会不断地问你的问题,直到所有的答案都是正确的。如果有任何问题被错误地回答,他们会再次被问到。