I stuck at the part about how many question they want for their game.This is the requrirement.

Write a program for an application that let the user host a quiz game. The application will have a collection of questions with short answers. The program should ask the user how many question they want for their game. It will then ask that many questions in random order. When the user types in an answer, it will check the answer against the correct answer and keep track of the number of questions they got right. It will never ask the same question twice. When all the questions have been asked, or the user has quit (by typing "quit" as an answer), the program will print the score and the total number of questions asked.

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 for your quiz game? "))
  #  while target > len(Questions) :
  #      print("Sorry, I only have ", len(Questions), " in my databank")
  #      target = int(input("How many questions for your quiz game? "))

    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()
happygeek commented: the return of the 'doesn't listen man' sigh... -2
TrustyTony commented: Time to do something yourself! -3

Recommended Answers

All 8 Replies

You want to use numberAsked in the while loop instead of
while len(Questions) > 0 :

Also, you can use random.shuffle on the Questions list and then take them in order instead of deleting and checking for length.

I don't get it

Which line should I edit?

Why did you flag it bad post? I did tried, but that is not one I want. The code you gave me does not allowed the people to type quit to quit in the middle of the game.

I did not flag it was happygeek,read Comments.

The code you gave me does not allowed the people to type quit to quit in the middle of the game.

I can change code so that is possible,but what's the logic in that?
The user get a chooice of how many question,if user choose 4 question the he/she get 4 question.
Maybe user get tired after 3 and want to quit.

commented: Up vote for excellent logic +13

i just do what they told me to do

could you teach me what should i do or what should i add to my coding?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.