My import random not working...... can anyone help me? and i need to make sure that people can quit in the middle of the quiz... THX

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

My hunch is that the problem dean.ong.14 (Dean Ong) encounters is caused by a user file saved as random.py, something to be avoided.

how to add quit?

# make input equal to Python3 style input even in Python2
import random


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'),
    # several lines omitted for clarity...
    ("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')]
random.shuffle(questions) # automagically rearrange the questions in a random order

if questions == "quit":
    print ("")
else:
    print("thanks for trying my quiz, Good bye")


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

Edited 4 Years Ago by dean.ong.14

This question has already been answered. Start a new discussion instead.