我正在嘗試獲取一個程式來為正確答案加分。我應該在三個單獨的串列中包含我的問題、正確答案和候選答案。這些問題一次一個地被問到,我必須回傳答案是否正確。然后我應該為每個正確答案加分。當我運行我的代碼時,它只在正確答案后顯示 1。我嘗試將 points 變數放在不同的地方,看看是否可行做。
我的功能如下。
def ask_questions():
#create lists for questions, correct answers, and candidate answers
question_1 = '1) Who was the first American woman in space? '
correct_answer_1 = 'Sally Ride'
candidate_answer_1 = input(question_1)
question_2 = '2) True or false: 5 kilometer == 5000 meters? '
correct_answer_2 = 'true'
candidate_answer_2 = input(question_2)
question_3 = '3) (5 3)/2 * 10 = ? '
correct_answer_3 = '40'
candidate_answer_3 = input(question_3)
question_4 = "4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? "
correct_answer_4 = 'Trajectory'
candidate_answer_4 = input(question_4)
question_5 = '5) What is the minimum crew size for the ISS? '
correct_answer_5 = '3'
candidate_answer_5 = input(question_5)
#lists that correlate to the variables assigned above
quiz_questions = [question_1, question_2, question_3, question_4, question_5]
correct_answers = [correct_answer_1, correct_answer_2, correct_answer_3, correct_answer_4, correct_answer_5]
candidate_answers = [candidate_answer_1, candidate_answer_2, candidate_answer_3, candidate_answer_4, candidate_answer_5]
index = 0
# points = 0
# total_score = (points/5) * 100
for item in quiz_questions:
points = 0
if candidate_answers[index].lower() == correct_answers[index].lower():
points = 1
print(f'Your Answer: {candidate_answers[index]}\nCorrect Answer: {correct_answers[index]}')
elif candidate_answers[index] != correct_answers[index]:
print('Incorrect.\nThe correct answer is: ', correct_answers[index])
index = 1
print(points)
ask_questions()
uj5u.com熱心網友回復:
主要問題是您要重置points到0回圈內部,這意味著它只能是0or 或1. 業務index令人困惑,可能難以除錯這些points東西;我建議只使用它zip來使整個事情變得更容易:
points = 0
for correct, candidate in zip(correct_answers, candidate_answers):
if correct.lower() == candidate.lower():
points = 1
print(f'Your Answer: {candidate}\nCorrect Answer: {correct}')
else:
print('Incorrect.\nThe correct answer is: ', correct)
print(points)
uj5u.com熱心網友回復:
Samwise 是正確的,回圈points的每次迭代都存在重新分配的問題for。但是,您可以通過將所有問答對存盤在一個元組串列中,然后一次遍歷它們以從用戶那里收集答案,從而使您的代碼更簡潔:
def ask_questions():
quiz_questions = [
('1) Who was the first American woman in space? ', 'Sally Ride'),
('2) True or false: 5 kilometer == 5000 meters? ', 'true'),
('3) (5 3)/2 * 10 = ? ', '40'),
("4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? ", 'Trajectory'),
('5) What is the minimum crew size for the ISS? ', '3')
]
submitted_answers = []
for question, _ in quiz_questions:
submitted_answers.append(input(question))
points = 0
for answer, (_, correct_answer) in zip(submitted_answers, quiz_questions):
if answer.lower() == correct_answer.lower():
points = 1
print(f'Your Answer: {answer}\nCorrect Answer: {correct_answer}')
else:
print('Incorrect.\nThe correct answer is: ', correct_answer)
print(points)
uj5u.com熱心網友回復:
您在回圈內宣告了點,問題在于每次迭代,點被重置回 0 并修復它只是將其移出 for 回圈。
從:
for item in quiz_questions:
points = 0
...
到:
points = 0
for item in quiz_questions:
...
但是我確實認為您的代碼可以寫得更干凈一些:
questions = [
{
"question": '1) Who was the first American woman in space? ',
"answer": 'Sally Ride'
},
{
"question": '2) True or false: 5 kilometer == 5000 meters? ',
"answer": 'true'
},
{
"question": '3) (5 3)/2 * 10 = ? ',
"answer": '40'
},
{
"question": "4) Given the list [8, 'Orbit', 'Trajectory', 45], what entry is at index 2? ",
"answer": 'Trajectory'
},
{
"question": '5) What is the minimum crew size for the ISS? ',
"answer": '3'
},
]
points = 0
for i in questions:
if input(i['question']).lower() == i['answer'].lower():
print('correct')
else:
print(f"Incorrect.\nThe correct answer is: {i['answer']}")
print(points)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460387.html
上一篇:如何將if-else陳述句中的資訊添加到陣列Java
下一篇:如何從檔案中批量恢復特定字串
