打印上新的生产线

问题描述:

这每一个选项是我到目前为止的代码:打印上新的生产线

questions = ["What is 1 + 1","What is Batman's real name"] 
answer_choices = ["1)1\n2)2\n3)3\n4)4\n5)5\n:","1)Peter Parker\n2)Tony Stark\n3)Bruce Wayne\n4)Thomas Wayne\n5)Clark Kent\n:"] 
correct_choices = ["2","3",] 
answers = ["1 + 1 is 2","Bruce Wayne is Batman"] 

score = 0 
answer_choices = [c.split()[:3] for c in answer_choices] 
for question, choices, correct_choice, answer in zip(questions,answer_choices, correct_choices, answers): 
    print(question) 
    user_answer = str(input(choices)) 
    if user_answer in correct_choice: 
     print("Correct") 
     score += 1 
    else: 
     print("Incorrect", answer) 
    print(score, "out of", len(questions), "that is", float(score /len(questions)) * 100, "%") 

我的代码运行,但answer_choices(所以问题的选项),不要每个列表元素的新线显示。我怎样才能做到这一点?如果您删除/注释行

answer_choices = [c.split()[:3] for c in answer_choices] 

,你想你会得到的输出的解释也将是不错

+0

请重新粘贴您的代码,并使用“{}”按钮将其缩进。很难说出你的'for'循环里面应该有什么。 – luther

你应该为了摆脱这一行,为您的代码工作:

answer_choices = [c.split()[:3] for c in answer_choices 

注意,你不这样做必须拆分answer_choices,因为您不会将每个问题的答案视为数组。

此外,你的代码中有更多的错误,比如最后的评分。以下是您的代码的格式化和固定版本:

questions = [ 
"What is 1 + 1?", 
"What is Batman's real name?"] 

answer_choices = [ 
"1) 1\n2) 2\n3) 3\n4) 4\n5) 5\n\nYour answer: ", 
"1) Peter Parker\n2) Tony Stark\n3) Bruce Wayne\n4) Thomas Wayne\n5) Clark Kent\n\nYour answer: "] 

correct_choices = ["2","3",] 

answers = [ 
"1 + 1 is 2", 
"Bruce Wayne is Batman"] 

score = 0 

for question, choices, correct_choice, answer in zip(
questions,answer_choices, correct_choices, answers): 
    print(question) 
    user_answer = str(input(choices)) 
    if user_answer in correct_choice: 
     print("Correct!\n") 
     score += 1 
    else: 
     print("Incorrect! " + answer + "\n") 

print score, "out of", len(questions), "that is", int(float(score)/len(questions)*100), "%" 

由于answer_choices已经是一个String数组,因此您在for循环中访问answer_choices的每个数组元素。另外,由于answer_choices中的每个字符串都是您需要显示的格式,因此您无需分割。

只是删除

answer_choices = [c.split()[:3] for c in answer_choices] 

它给你的预期输出