2017-10-05 23 views
1

我正在寫這個python代碼,它將允許用戶選擇一個簡單和困難的模式,並有多種選擇。每種模式的問題都是一樣的,但硬版本在每個問題中都有更多的選項可供選擇。這是到目前爲止我的代碼:在測驗蟒蛇上製作一個簡單而困難的模式

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"] 

def quiz(): 
    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") 
      score += 1 
     else: 
      print("Incorrect", answer) 
    print(score, "out of", len(questions), "that is", float(score /len(questions)) * 100, "%") 

quiz() 

我將如何添加簡單的和困難多而不進行新的列表,並具有複製和粘貼的一切嗎?解釋也很好。 預先感謝任何答覆

+0

您需要編寫基於簡單模式列表和其他附加信息(附加選項)生成硬模式列表的代碼。做到這一點的最佳方式大多是意見。 –

+0

你需要查看字典。 – Chris

+0

你應該考慮將'布魯斯·韋恩是蝙蝠俠'改寫爲'蝙蝠俠是布魯斯·韋恩'給出瞭如何提出的問題:) –

回答

2

您可以創建所有問題的列表,然後根據需要拼接它。

def get_choices(difficulty): 
    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:" 
    ] 

    if difficulty == 'easy': 
     choices = [c.split("\n")[:3] for c in choices] 
     return choices 
    elif difficulty == 'medium': 
     choices = [c.split("\n")[:4] for c in choices] 
     return choices 
    else: 
     return choices 

如果你可以讓每個單獨的選擇一個列表元素,並有一個解決方案對應它,它會更簡單。然後,您可以得到正確的解決方案並隨機播放其他答案並自動分配號碼。

+0

工作正常,但我將如何打印新行上的每個元素?因此,打印時,選擇中的每個元素都會換行。我想我可以通過 – C4RN4GE

+0

來完成剩下的工作,你可以改變這個'user_answer = str(input(choices))'來迭代打印所有的選擇,然後執行'user_answer = input()'或者使用''\ n '.join(選擇)'如果你想保持這條線。 – 4d11

0

可以定義功能塊,並呼籲他們根據用戶輸入的類似:

# define the function blocks 
def hard(): 
    print ("Hard mode code goes here.\n") 

def medium(): 
    print ("medium mode code goes here\n") 

def easy(): 
    print ("easy mode code goes here\n") 

def lazy(): 
    print ("i don't want to play\n") 

# Now map the function to user input 
choose_mode = {0 : hard, 
      1 : medium, 
      4 : lazy, 
      9 : easy, 

} 
user_input=int(input("which mode do you want to choose : \n press 0 for hard \n press 1 for medium \n press 4 for lazy \n press 9 for easy ")) 
choose_mode[user_input]() 

然後功能塊的調用將是:

choose_mode[num]() 
0

一實現類似這樣的方式的方式是從一個列表開始,該列表包含每個問題的正確答案和可能的錯誤答案。

然後,您將根據難度級別爲每個問題挑選正確數量的錯誤問題,從而創建可從該基礎列表生成實際問題列表的代碼。然後生成的新列表將用於提出問題。