2012-11-27 52 views
-4

我正在創建一個測驗...我想確保人們可以選擇他們想要回答的問題數量。 編寫一個程序,讓用戶主持一個測驗遊戲。該應用程序將收集一系列簡短答案的問題。該程序應該詢問用戶他們想要的遊戲有多少個問題。然後它會隨機地詢問很多問題。當用戶輸入答案時,它會根據正確答案檢查答案,並跟蹤他們正確答案的數量。它永遠不會問同樣的問題兩次。當所有的問題都已經問,或用戶已經退出(通過輸入「跳槽」作爲回答),該程序將打印的分數和問題總數問我被困在T.T的一個部分,任何人都可以幫助我嗎?

from random import * #random is a library. we need the randint function from it 

    def Main() : 
     Questions = [] # list of all the questions 
     Answers = [] # list of all the answers 
     setup(Questions, Answers) 

     while True : 
      target = int(input("How many questions do you want to answer? more than 1 and less than 11 ")) 
      if target <= len(Questions) : 
       break 
      print("Sorry, I only have ", len(Questions), " in my databank") 

     # alternate version: 
     # target = int(input("How many questions do you want to answer? ")) 
     # while target > len(Questions) : 
     # print("Sorry, I only have ", len(Questions), " in my databank") 
     # target = int(input("How many questions do you want to answer? ")) 
     # 

     score = 0 
     numberAsked = 0 
     while len(Questions) > 0 : 
      qnNum = randint(0, len(Questions)-1) 
      correct = askQuestion(Questions[qnNum], Answers[qnNum]) 
      numberAsked = numberAsked + 1 
      if correct == "quit" : 
       break 
      elif correct : 
       score=score+1 
      del Questions[qnNum] 
      del Answers[qnNum] 
     reportScore(score, numberAsked) 

    def reportScore(sc, numAsked) : 
     print("Thanks for trying my quiz, Goodbye", sc, " questions right out of ", numAsked) 


    #asks the user a question, and returns True or False depending on whether they answered correctly. 
    # If the user answered with 'q', then it should return "quit" 
    def askQuestion (question, correctAnswer): 
     print(question) 
     answer = input("your answer: ").lower() 
     if answer == "quit" : 
      return "quit" 
     elif answer == correctAnswer.lower() : 
      print("Well done, you got it right!") 
      return True 
     else : 
      print("You got it wrong this time!. The correct answer is ", correctAnswer) 
      return False 

    # Sets up the lists of questions 
    def setup(Questions, Answers) : 
     Questions.append("The treaty of Waitangi was signed in 1901") 
     Answers.append("FALSE") 
     Questions.append("Aotearoa commonly means Land of the Long White Cloud") 
     Answers.append("TRUE") 
     Questions.append("The Treaty of Waitangi was signed at Parliament") 
     Answers.append("FALSE") 
     Questions.append("The All Blacks are New Zealands top rugby team") 
     Answers.append("TRUE") 
     Questions.append("Queen Victoria was the reigning monarch of England at the time of the Treaty") 
     Answers.append("TRUE") 
     Questions.append("Phar Lap was a New Zealand born horse who won the Melbourne Cup") 
     Answers.append("TRUE") 
     Questions.append("God Save the King was New Zealand’s national anthem up to and including during WWII") 
     Answers.append("TRUE") 
     Questions.append("Denis Glover wrote the poem The Magpies") 
     Answers.append("TRUE") 
     Questions.append("Te Rauparaha is credited with intellectual property rights of Kamate!") 
     Answers.append("FALSE") 
     Questions.append("Kiri Te Kanawa is a Wellington-born opera singer") 
     Answers.append("FALSE") 

    Main() 
+2

你的問題是什麼? – BrenBarn

+0

我正在創建一個測驗......我想確保人們可以選擇他們想要回答的問題數量。這是我從phyton shell獲得的結果。 –

+0

你想回答多少個問題? 1比1以下11 Phar Lap是新西蘭出生的馬誰贏得了墨爾本杯 你的回答: 你錯了這一次!正確的答案是TRUE Aotearoa通常意味着長白雲之地 你的回答: 這次你錯了!正確的答案是TRUE Waitangi條約在議會 上簽字您的回答: 這次你錯了!正確的答案是FALSE Kiri Te Kanawa是惠靈頓出生的歌劇歌手 你的回答: 這次你錯了!正確的答案是FALSE –

回答

1

的主要問題是在你的while循環 - 你沒有做任何事情target,這應該是管理問題的數量。如果沒有事建議的修改太瘋狂了,試着用這個代替周圍的while循環的代碼:

score = 0 
numberAsked = 0 
while numberAsked < target: 
    qnNum = randint(0, len(Questions)) 
    correct = askQuestion(Questions[qnNum], Answers[qnNum]) 
    numberAsked = numberAsked + 1 
    if correct == "quit" : 
     break 
    elif correct : 
     score=score+1 
    del Questions[qnNum] 
    del Answers[qnNum] 

這將循環而numberAsked小於target。你目前的問題是,你的循環由Questions列表的長度控制,該列表從10開始,每次迭代減1。因此,不管你的target是什麼,循環將循環所有的問題。

+0

回溯(最近通話最後一個): 文件 「d:\ Python的assesment.py」 22行,在 而目標> 0: NameError:名字 '目標' 沒有定義 –

+0

@DeanOng對不起,我的意思是用'score = 0'開始替換你的代碼塊,並用'del Answers [qnNum]'結束。爲了使它更容易,在你的代碼中,你可以用'while numAnswered 0'。 – RocketDonkey

+0

回溯(最近通話最後一個): 文件 「d:\ Python的assesment.py」 22行,在 而目標> 0: NameError:名字 '目標' 沒有定義 –

相關問題