-4

我正在创建一个测验...并且我想确保人们可以选择他们想要回答多少个问题.. 为一个应用程序编写一个程序,让用户托管一个测验游戏。该应用程序将包含一系列带有简短答案的问题。程序应该询问用户他们想为他们的游戏提出多少问题。然后它将以随机顺序询问许多问题。当用户输入答案时,它会根据正确答案检查答案,并跟踪他们答对的问题数量。它永远不会两次问同一个问题。当所有的问题都被问完,或者用户已经退出(通过输入“quit”作为答案),程序将打印分数和提出的问题总数

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()
4

1 回答 1

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]

这将循环 whilenumberAsked小于target。您当前的问题是您的循环由Questions列表的长度控制,该长度从 10 开始,每次迭代减少 1。因此,无论您target是什么,循环都会遍历所有问题。

于 2012-11-27T08:21:30.243 回答