Question

I'm very very new to python, and I need to make it so my program randomly selects a question from a trivia game, but doesnt repeat it afterwards. The game is based from questions from a book about python I recieved.

print "trivia game"
begin = raw_input("Would you like to begin?: ")
if begin == "Yes":
    print "A) Getting your computer to do stuff"
    print "B) A form of watching television"
    print "C) Building a computer"
    print "D) Assembling a computer with your bare hands"
    q1 = raw_input("What is programming?: ")
    if q1 == "A" or q1 == "a":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) African Standard Code For Information Interchange"
    print "B) American Standard Code For Information Interchange"
    print "C) American Subtle Codes For Information Interchange"
    print "D) African Subtle Codes For Inside Interchange"
    print " "
    q2 = raw_input("What does ASCII stand for?: ")
    if q2 == "b" or q2 == "B":
        print "Well done! You got it right! "
        print " "
    else:
        print "no, you're wrong! +0"
    print "A) A hidden group of words"
    print "B) A group of random letters"
    print "C) The generation of random numbers by a definite computational process. "
    print "D) The way to write code"
    q3 = raw_input("What is pseudorandom?: ")
    if q3 == "C" or q3 == "c":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) Only strings and loops"
    print "B) Only loops"
    print "C) Only strings"
    print "D) Any type"
    q4 = raw_input("What kinds of elements can you store with a tuple?: ")
    if q4 == "D" or q4 == "d":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) Yes"
    print "B) No"
    print "C) Only if they are loops"
    print "D) Only if they are strings"
    q5 = raw_input("Do variables store the values you assign to them?: ")
    if q5 == "A" or q5 == "a":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) One"
    print "B) There is no limit"
    print "C) 158,240"
    print "D) 1000"
    q6 = raw_input("How many paramters can a function have?: ")
    if q6 == "B" or q6 == "b":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) Lists of numbers"
    print "B) Complex information"
    print "C) Simple information"
    print "D) Loops"
    q7 = raw_input("What type of information wold be best stored in a text file?: ")
    if q7 == "C" or q7 == "c":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) To write a program with over 1000 lines of code"
    print "B) To create a program that write programs"
    print "C) To create program after program"
    print "D) To create an instance of an object in an object-oriented programming (OOP) language."
    q8 = raw_input("What does instantiate mean?: ")
    if q8 == "D" or q8 == "d":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) It allows you to send the same message to objects of different classes related by inheritance and achieve different and appropriate results"
    print "B) It lets you combine two programs"
    print "C) It creates polygons"
    print "D) It allows you to repeat a loop an infinite amount of times"
    q9 = raw_input("How can polymorphism help a programmer?: ")
    if q9 == "A" or q9 == "a":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) grid()"
    print "B) create_widgets()"
    print "C) root.title"
    print "D) root.mainloop()"
    q10 = raw_input("What widget method sets a widget's options?: ")
    if q10 == "B" or q10 == "b":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) A compass"
    print "B) A graph"
    print "C) A grid"
    print "D) A virtual loop system"
    q11 = raw_input("What is a graphics coordinate system?: ")
    if q11 == "C" or q11 == "c":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) A file with text about sound"
    print "B) A looping sound"
    print "C) A radio channel"
    print "D) Where a sound is stored"
    q12 = raw_input("What's a sound channel?: ")
    if q12 == "D" or q12 == "d":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q13 = raw_input("Can Python increase programmer productivity?: ")
    if q13 == "a" or q13 == "A":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q14 = raw_input("It's not important to use the right types in your programs.: ")
    if q14 == "b" or q14 == "B":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q15 = raw_input("You should plan your programs.: ")
    if q15 == "A" or q15 == "a":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q16 = raw_input("Constants aren't helpful.: ")
    if q16 == "b" or q16 == "B":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q17 = raw_input("Dictionary keys must be unique.: ")
    if q17 == "a" or q17 == "A":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q18 = raw_input("You should never use docstrings.: ")
    if q18 == "b" or q18 == "B":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q19 = raw_input("You should avoid a catch-all exception handling.: ")
    if q19 == "a" or q19 == "A":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q20 = raw_input("An object should never update its private attributes.: ")
    if q20 == "b" or q20 == "B":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q21 = raw_input("You should include only related programming elements in a single module.: ")
    if q21 == "a" or q21 == "A":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q22 = raw_input("You can't test which radio button in a group is selected.: ")
    if q22 == "b" or q22 == "B":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q23 = raw_input("You should create a batch file to run your graphics programs.: ")
    if q23 == "a" or q23 == "A":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
    print "A) True"
    print "B) False"
    q24 = raw_input("There is no such thing as a sound channel.: ")
    if q24 == "b" or q24 == "B":
        print "Well done you got it right! +1"
        print " "
    else:
        print " no, you're wrong! +0"
else:
    print "Invalid answer."
Was it helpful?

Solution

You can do that as something as follows:

from random import shuffle
list_of_questions = ['A', 'B', 'C', 'D', 'E', and so on]

shuffle(list_of_questions)

question = list_of_questions.pop()

The .pop() method deletes the last question from the list and returns it. In this case it will always be randomized since that is what shuffle does therefore it doesn't matter that it is always the last.

For example:

>>> some_list = [1,2,3,4,5]
>>> shuffle(some_list)
>>> some_list
[4,2,5,1,3]
>>> some_list.pop()
3
>>> some_list
[4,2,5,1]

[NOTE]

I also suggest that you create a separate text file with all the questions (1 question per line). Using python, you an easily read in those questions and at a future date, you can also easily delete or add new questions without much effort.

Example:

#questions.txt         Remember, your questions file will need to be in the same directory as your python script
What is ..?
Who is ..?
Where is ..?
Why is ...?


#Start of your python code
questions = [line.strip() for line in open('questions.txt'. 'r')]


>>> print questions
['What is ...?', 'Who is ...?', 'Where is ...?', 'Why is ...?']

>>> from random import shuffle
>>> shuffle(questions)
>>> print questions.pop()
Where is ...?
>>> print questions
['What is ...?', 'Who is ...?', 'Why is ...?'] #Note the question that was taken is gone from the list. Thanks to the .pop() method

OTHER TIPS

Try it with a combination of the csv module and some custom syntax.

Here's an example csv file:

"What is programming?","*Getting your computer to do stuff","A form of watching television","Building a computer","Assembling a computer with your bare hands"
"What does ASCII stand for?","African Standard Code For Information Interchange","*American Standard Code For Information Interchange","American Subtle Codes For Information Interchange","African Subtle Codes For Inside Interchange"
"What is pseudorandom?","A hidden group of words","A group of random letters","*The generation of random numbers by a definite computational process","The way to write code"
"What kinds of elements can you store with a tuple?","Only strings and loops","Only loops","Only strings","*Any type"
...

Note that the first item in each line is the question, and then the rest are answers. The "correct" answer is indicated by a * at the beginning. The program will be able to parse this file easily, and any human editor can add or edit questions trivially.

Here's a program that encapsulates the game in a function, uses the csv module to read the questions and answers from a file, keeps track of score in a variable, and automatically parses which answer is "correct" from the * at the beginning of the correct answer in the file:

import csv

def play_trivia(questions_path):
    # start the game
    print 'Trivia game:'
    if raw_input("Would you like to begin? (Y/N): ").lower() != 'y':
        print "Goodby."
        return
    print
    score = 0
    # set choice letters (and order)
    letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    # loop through each row in the file
    for row in csv.reader(open(questions_path, 'r')):
        # print the question
        print row[0]
        correct_choice = 'A' # just in case csv is missing the *
        # loop through answers
        # (Start with element 1, since element 0 was the question.)
        for i, choice in enumerate(row[1:]):
            if choice.startswith("*"):
                correct_choice = letters[i]
                print letters[i] + ") " + choice[1:]
            else:
                print letters[i] + ") " + choice
        # ask for the correct answer
        if raw_input("Enter answer: ").upper() == correct_choice:
            score += 1
            print "Well done you got it right! +1 (Score is now " + str(score) + ")"
        else:
            score -= 1
            print "No, you're wrong! -1 (Score is now " + str(score) + ")"
        # separate questions with a line for readability
        print
    # and now we're done
    print "Final score: " + str(score)

Of minor note, letters[i] means the ith letter in letters.

This worked quite well in my python interpreter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top