【发布时间】:2021-05-25 15:15:26
【问题描述】:
关于节目:
在这个程序中,我需要先获取用户的输入以进行“测验” 问号。的问题,然后问问题文本和 4 'answers' ,并从这 4 个答案中要求正确答案。 重复没有。问题。
就像一个“测验制作者”
list_of_questions 存储问题的文本
list_of_keys 存储所有问题的输入“答案”(按顺序) 示例:
list_of_keys = [Q1_Answer1, Q1_Answer2, Q1_Answer3, Q1_Answer4, Q2_Answer1, Q2_Answer2, Q2_Answer3, Q2_Answer4, Q3_Answer1 etc...]
在 start_quiz() 中,我需要先打印第一个问题文本和 4 个答案,然后打印第二个问题文本和 4 个答案等...
使用循环从列表中获取特定项目
但是我在下面制作的循环:只是再次打印相同的前 4 个答案。
我找不到从列表中正确获取项目的解决方案。
我试过了,但没用:(超出范围错误)
var1 = 0
for I in main_list:
answer_amount = 4
if answer_amount > 0:
var1 += 1
print(list_of_keys[var1])
整个代码:
from questionclass import Question_template
main_list = []
list_of_questions = []
list_of_answers = []
list_of_keys = []
print("\n Quiz maker \n")
def make_quiz():
X = 0
Z = 0
Y = 0
amount_of_answers = 4
question_amount = int(input(" Enter number of questions for your quiz: "))
while question_amount > 0:
question_amount -= 1
X += 1
list_of_questions.append(str(input(" Text for question no." + str(X) + ": ")))
while amount_of_answers > 0:
amount_of_answers -= 1
Y += 1
list_of_keys.append((str(Y) + ".") + str(input(" Answer no." + str(Y) + ": ")))
amount_of_answers = 4
list_of_answers.append(int(input("\nwhich answer is the correct answer?\n")))
Y = 0
for question in list_of_questions:
main_list.append(Question_template(question, list_of_keys[Z], list_of_answers[Z]))
Z += 1
start_quiz()
def start_quiz():
key = int(input("\n enter 0 to start quiz: "))
score = 0
If key == 0:
for question in main_list:
print(question.promt) # printing the question text
amount_of_answers = 4
for i in list_of_keys: ####THIS LOOP HERE####
if amount_of_answers > 0:
print(i)
amount_of_answers -= 1
answer = int(input(""))
if answer == list_of_answers[0]:
score += 1
makequiz()
问题类别:
class Question_template:
def __init__(self, promt, answers, correct):
self.promt = promt
self.answers = answers
self.correct = correct
【问题讨论】:
-
我的建议是以更简单的形式分解代码或将它们分开。对于初学者,你有一个语法错误,你使用 'IF' 而不是 if
-
语法错误在哪一行??
-
如果 key == 0: start_quiz 中的第 3 行
-
当我复制粘贴代码时,我一定是不小心把它大写了
标签: python