本文介绍了将用户输入列表按顺序与重复项进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试以特定顺序(例如多项选择测试)取A,B或D的答案,并让用户输入他的答案。之后,我想它创建第三个列表,打印出什么是正确的和错误的。这是我到目前为止。

  userAnswersList = [] 
correctAnswers = ['A','C' ,A,A,D,B,C,A,C,B当用户AnswersList userAnswers = input('''''''''给我每个答案总共20个问题我会让你知道你有多少你错过了。)
userAnswersList.append(userAnswers.upper())

correctedList = []
for i in userAnswersList:
if i in correctAnswers:
correctedList.append(i)
else:
correctedList.append('XX')
因此,我的最终结果将是具有'X'的已更正列表,其中'X'在那里他们错过了答案,如果是正确的,它只是把用户输入的地方。
所以在用户输入他们的20个答案后,它将看起来像
['A','C','A','XX','D','B','C' XX','C','B','A','XX','C','A','D','XX','B','B' ]
如果他们在该订单中漏了5个问题



b

再次感谢您的帮助,我能够解决我的问题,您的帮助和一些很好的答案。我使用Nicks解决方案,因为这是我们如何学习它。



我会尝试其他人,所以我可以习惯他们。

解决方案

在userAnswersList中不使用:

  

您可能会发现更容易遍历数组并检查值是否相等,例如:

  for i in range(len(userAnswersList)):
if userAnswersList [i] == correctAnswers [i] :
correctedList.append(userAnswersList [i])
else:
correctedList.append('XX')


I am trying to take a set of answers either 'A' 'B' 'C' or 'D' in a specific order such as a multiple choice test and have the user input his answers. After I would like it to create a third list and print out what was right and wrong. Here is what I have so far.

 userAnswersList = []
 correctAnswers = ['A', 'C', 'A', 'A', 'D', 'B', 'C', 'A', 'C', 'B', 'A', 'D', 'C', 'A', 'D', 'C', 'B', 'B', 'D', 'A']

 while len(userAnswersList) <= 19:
     userAnswers = input('Give me each answer total of 20 questions I\'ll let  you know how many you missed.')
     userAnswersList.append(userAnswers.upper())

correctedList = []
for i in userAnswersList:
    if i in correctAnswers:
        correctedList.append(i)
    else:
        correctedList.append('XX')

print(correctedList)

So my end result would be the corrected list with a 'X' in place where they missed the answer, If it is right it just puts the user input in that place.So after the user input their 20 answers it would look like['A', 'C', 'A', 'XX', 'D', 'B', 'C', 'XX', 'C', 'B', 'A', 'XX', 'C', 'A', 'D', 'XX', 'B', 'B', 'XX', 'A']if they missed 5 questions in that order

EDIT

Thank you again for all your help I was able to solve my problems with your help and some great answers. I used Nicks solution as that is how we are learning it.

I will try out others just so I can get used to them.

解决方案

Rather than using:

for i in userAnswersList:

you may find it easier to iterate through the array and check if the values are equal, such as:

for i in range(len(userAnswersList)):
    if userAnswersList[i] == correctAnswers[i]:
        correctedList.append(userAnswersList[i])
    else:
        correctedList.append('XX')

这篇关于将用户输入列表按顺序与重复项进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:03