i need help. i want to make sure that people can ask how many question the want to answer in my quiz. how can i do that?

# make input equal to Python3 style input even in Python2
try:
    input = raw_input
except:
    pass


print("Welcome to my yes or no quiz!")
questions = (("Denis Glover wrote the poem The Magpies ", 'yes'),
             ("God Save the King was New Zealand’s national anthem up to and including during WWII  ", 'yes'),
             ("Kiri Te Kanawa is a Wellington-born opera singer  ", 'no'),
             ("Phar Lap was a New Zealand born horse who won the Melbourne Cup  ", 'yes'),
             ("Queen Victoria was the reigning monarch of England at the time of the Treaty  ", 'yes'),
             ("Split Enz are a well-known rock group from Australia who became very famous in New Zealand during the 80s  ", 'no'),
             ("Te Rauparaha is credited with intellectual property rights of Kamate!  ", 'yes'),
             ("The All Blacks are New Zealands top rugby team ", 'yes'),
             ("The Treaty of Waitangi was signed at Parliament  ", 'no'),
             ("The Treaty of Waitangi was signed in 1901 ", 'no'),
             ("Aotearoa commonly means Land of the Long White Cloud   ", 'yes'))


for q, right in questions:
    if input(q).lower() == right:
        print("Well done! You got it right!")
    else:
        print("You got it wrong this time!")

Recommended Answers

All 5 Replies

I would try asking the user for the number of questions to ask before the for: loop, and use enumerate() to get a count of how many questions have been asked. Then, add an if: clause that checks whether you have reached the number of questions, and break if you have.

The enumerate() is a bit tricky, so I'll give that part to you:

for count, (q, right) in enumerate(questions):

The reason for this is because enumerate() returns a tuple containing the enumerated value, and the next value in the argument. Since the next value in questions is itself a tuple, you need the parens around the two variables to force it to unpack the inner tuple.

which line should i put the code in?
how to loop and how to add score?
or if possible could u edit my coding? thx

Adding a score is fairly easy; all you need to do is have a variable score which is set to zero to start, then for every right answer, add one to it.

# make input equal to Python3 style input even in Python2
try:
    input = raw_input
except:
    pass


print("Welcome to my yes or no quiz!")
questions = (("Denis Glover wrote the poem The Magpies ", 'yes'),
             ("God Save the King was New Zealand’s national anthem up to and including during WWII  ", 'yes'),
             ("Kiri Te Kanawa is a Wellington-born opera singer  ", 'no'),
             ("Phar Lap was a New Zealand born horse who won the Melbourne Cup  ", 'yes'),
             ("Queen Victoria was the reigning monarch of England at the time of the Treaty  ", 'yes'),
             ("Split Enz are a well-known rock group from Australia who became very famous in New Zealand during the 80s  ", 'no'),
             ("Te Rauparaha is credited with intellectual property rights of Kamate!  ", 'yes'),
             ("The All Blacks are New Zealands top rugby team ", 'yes'),
             ("The Treaty of Waitangi was signed at Parliament  ", 'no'),
             ("The Treaty of Waitangi was signed in 1901 ", 'no'),
             ("Aotearoa commonly means Land of the Long White Cloud   ", 'yes'))

score = 0
maxQuestions = 0
while maxQuestions <= 0 or maxQuestions > len(questions):
    try:
        maxQuestions = int(input("Enter the number of questions to ask (more than 0 and less than to {0}): ".format(len(questions))))
    except ValueError:
        print("Please enter an integer value.")
        maxQuestions = 0

for count, (q, right) in enumerate(questions):
    if count >= maxQuestions:
        break
    elif input(q).lower() == right:
        print("Well done! You got it right!")
        score += 1
    else:
        print("You got it wrong this time!")

print("Your score is {} points".format(score))

thank you so much!!!!!! last question.... how to extend the quiz to add more user friendly and robust features? an how to add comment?

i want to make sure that people can quit in the middle of the quiz and i want the question to be random... help me please

import random
# make input equal to Python3 style input even in Python2
def question ():

try:
    input = raw_input
except:
    pass
question =" "
print("Welcome to my yes or no quiz!")
#set up a variable lists
while question !="quit":

question = (("Denis Glover wrote the poem The Magpies ", 'yes'),
    ("God Save the King was New Zealand’s national anthem up to and including during WWII ", 'yes'),
    ("Kiri Te Kanawa is a Wellington-born opera singer ", 'no'),
    ("Phar Lap was a New Zealand born horse who won the Melbourne Cup ", 'yes'),
    ("Queen Victoria was the reigning monarch of England at the time of the Treaty ", 'yes'),
    ("Split Enz are a well-known rock group from Australia who became very famous in New Zealand during the 80s ", 'no'),
    ("Te Rauparaha is credited with intellectual property rights of Kamate! ", 'yes'),
    ("The All Blacks are New Zealands top rugby team ", 'yes'),
    ("The Treaty of Waitangi was signed at Parliament ", 'no'),
    ("The Treaty of Waitangi was signed in 1901 ", 'no'),
    ("Aotearoa commonly means Land of the Long White Cloud ", 'yes'))
if question == "quit":break

score = 0
maxQuestions = 0
while maxQuestions <= 0 or maxQuestions > len(questions):
    try:
        maxQuestions = int(input("Enter the number of questions to ask (more than 0 and less than to {0}): ".format(len(questions))))
    except Exception as e:
        print(e)
        print("Please enter an integer value.")
        maxQuestions = 0
for count, (q, right) in enumerate(questions):
    if count >= maxQuestions:
        break
    elif input(q).lower() == right:
        print("Well done! You got it right!")
        score += 1
    else:
        print("You got it wrong this time!")
print("Your score is {} points".format(score))
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.