[英]Iterating through lists in python
我正在制作一个程序,首先以测试答案的形式接收用户输入。 然后它要求一个答案键。 最后,它应该比较列表中的每个项目并确定等级。
这是我的程序的示例运行,希望它能在其中运行:
How many questions are there? >> 5
What is your answer for question #1 >> a
What is your answer for question #2 >> a
What is your answer for question #3 >> a
What is your answer for question #4 >> a
What is your answer for question #5 >> a
What is the correct answer for question #1 >> a
What is the correct answer for question #2 >> a
What is the correct answer for question #3 >> a
What is the correct answer for question #4 >> a
What is the correct answer for question #5 >> d
Total number correct : 4
Total number possible: 5
Grade = 4.0/5.0, 0.8%
这是实际发生的事情:
How many questions are there? >> 5
What is your answer for question #1 >> a
What is your answer for question #2 >> a
What is your answer for question #3 >> a
What is your answer for question #4 >> a
What is your answer for question #5 >> a
Responses:
1. a
2. a
3. a
4. a
5. a
What is the correct answer for question #1 >> c
What is the correct answer for question #2 >> a
What is the correct answer for question #3 >> a
What is the correct answer for question #4 >> a
What is the correct answer for question #5 >> a
Responses:
1. c
2. a
3. a
4. a
5. a
a = c ?
a = a ?
a = a ?
a = a ?
a = a ?
a = c ?
a = a ?
a = a ?
a = a ?
a = a ?
a = c ?
a = a ?
a = a ?
a = a ?
a = a ?
a = c ?
a = a ?
a = a ?
a = a ?
a = a ?
a = c ?
a = a ?
a = a ?
a = a ?
a = a ?
20
Number of correct answers = 20 out of 5
Your score is: -3.0
我不确定为什么会这样,这可能是我在评分中遍历两个列表的方式吗? 这是我的代码:
class Grader:
def grade(self):
# get number of questions
numOfQuestions = input("How many questions are there? >> ")
# start gathering answers
i = 1
responses = []
while i <= int(numOfQuestions):
entry = input("\nWhat is your answer for question #" + str(i) + " >> ")
responses += entry
i += 1
# display user responses
print("\nResponses:\n")
j = 1
for r in responses:
print(str(j) + ". " + r)
j+=1
# grade the responses
# input answer key
x = 1
answers = []
while x <= int(numOfQuestions):
aentry = input("\nWhat is the correct answer for question #" + str(x) + " >> ")
answers += aentry
x += 1
# display answer key
print("\nResponses:\n")
y = 1
for z in answers:
print(str(y) + ". " + z)
y+=1
# time to actually grade the exam
numCorrect = 0
for p in responses:
for q in answers:
print(p+" = " +q+" ?")
if p == q:
numCorrect += 1
# issue a grade
print(str(numCorrect))
print("Number of correct answers = " + str(numCorrect) + " out of " + str(numOfQuestions))
grade = int(numOfQuestions) - int(numCorrect)
grade = grade / int(numOfQuestions)
print("Your score is: " + str(grade))
if __name__ == "__main__":
a = Grader()
a.grade()
它在这里:
numCorrect = 0
for p in responses:
for q in answers:
print(p+" = " +q+" ?")
if p == q:
numCorrect += 1
那是将每个响应与每个答案进行比较。 您只想将每个响应与其对应的答案进行比较。 最简单的方法是使用zip function,例如:
numCorrect = 0
for p, q in zip(responses, answers):
print(p+" = " +q+" ?")
if p == q:
numCorrect += 1
为了进行比较,您可以使用 zip 方法,我们可以同时循环两个列表这里是问题循环的答案比较的一个小变化
for p,q in zip(responses,answers):
print(p+" = " +q+" ?")
if p == q:
numCorrect += 1
class Grader:
def grade(self):
# get number of questions
numOfQuestions = input("How many questions are there? >> ")
# start gathering answers
i = 1
responses = []
for i in range(0,int(numOfQuestions)):
entry = input("\nWhat is your answer for question #" + str(i+1) + " >> ")
responses.append(entry)
# display user responses
print("\nResponses:\n")
for i in range(0,len(responses)):
print(str(i) + ". " + responses[i])
# grade the responses
# input answer key
answers = []
for i in range (0,len(responses)):
aentry = input("\nWhat is the correct answer for question #" + str(i+1) + " >> ")
answers.append(aentry)
# display answer key
print("\nResponses:\n")
for i in range(0, len(answers)):
print(str(i) + ". " + answers[i])
# time to actually grade the exam
numCorrect = 0
for i in range(0, len(answers)):
print(str(i)," = ",answers[i]," ?")
if responses[i] == answers[i]:
numCorrect += 1
# issue a grade
print(str(numCorrect))
print("Number of correct answers = " + str(numCorrect) + " out of " + str(numOfQuestions))
grade = int(numOfQuestions) - int(numCorrect)
if grade==int(0.0):
print("Scored 100%")
else:
grade = int(numCorrect)/int(numOfQuestions)
print("Your score is: ",grade)
if __name__ == "__main__":
a = Grader()
a.grade()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.