为了账号安全,请及时绑定邮箱和手机立即绑定

如何在每个循环中打印列表的不同元素?

如何在每个循环中打印列表的不同元素?

慕尼黑的夜晚无繁华 2024-01-15 17:04:03
我正在制作一个测验程序,用户可以在测验中添加任意数量的问题,并且他们也可以参加测验。我只是不知道如何单独打印问题。questionsList = [] rightAnswersList = []choicesList = []while True:    option = int(input("Add questions or take the quiz? 1 or 2 respectively: "))    if option == 1:       # adds a question to questionsList and choices to choicesList and answers to rightAnswersList     elif option == 2:       # prints question1 --> choices --> asks answer then it loops so question2 and its choices will now be printed
查看完整描述

1 回答

?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

我不确定你想要什么,但这满足了你的评论所要求的最低限度:


questionsList = [] 

rightAnswersList = []

choicesList = []

while True:

  option = int(input("Add questions or take the quiz? 1 or 2 respectively: "))

  if option == 1:

    questionsList.append(input("Enter question: "))

    choices = []

    while True:

      choice = input("Enter answer choice or enter 'q' to exit: ")

      if choice == "q":

        break

      choices.append(choice)

    choicesList.append(choices)

    rightAnswersList.append(input("Enter answer: "))

  elif option == 2:

    for i in range(len(questionsList)):

      print(questionsList[i])

      print("Choices:\n" + "\n".join(choicesList[i]))

      answer = input("Enter your answer: ")

      print("Your answer is: " + answer)

      print("Correct answer is: " + rightAnswersList[i])

您可能需要添加额外的代码来过滤用户输入或检查用户答案。


查看完整回答
反对 回复 2024-01-15
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信