2017-10-06 63 views
0

這每一個選項是我到目前爲止的代碼:打印上新的生產線

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

回答

1

你應該爲了擺脫這一行,爲您的代碼工作:

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), "%" 
0

由於answer_choices已經是一個String數組,因此您在for循環中訪問answer_choices的每個數組元素。另外,由於answer_choices中的每個字符串都是您需要顯示的格式,因此您無需分割。

0

只是刪除

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

它給你的預期輸出