Creating a flashcard-like quiz game (Python)

vegaseat 2 Tallied Votes 7K Views Share

This shows you how to create a flashcard like quiz game using a Python dictionary. It's up to you to explore the approach and make this a more meaningful game.

''' quiz_dictionary_style101.py
using a keyword:definitions_list dictionary for a flashcard-like quiz
tested with Python27 and Python33  by  vegaseat  08nov2013
'''

import random

# make string input work with Python2 or Python3
try: input = raw_input
except: pass

# a simple sample dictionary 'pick the right color'
# keyword:[def1, def2, def3, correct_def_ix]
# could use module pickle to dump/load this dictionary
mydict = {
'sun' : ['black', 'yellow', 'blue', 1],
'water' : ['red', 'white', 'blue', 2],
'grass' : ['white', 'green', 'orange', 1],
'coal' : ['black', 'purple', 'red', 0]
}

keyword_list = list(mydict.keys())
# shuffle the keywords in place
random.shuffle(keyword_list)

print('Pick the right color:')
correct = 0
wrong = 0
for keyword in keyword_list:
    sf = '''\
{}
A) {}
B) {}
C) {}
'''
    print(sf.format(keyword, mydict[keyword][0],
                    mydict[keyword][1],mydict[keyword][2]))
    letter = input("Enter letter of your choice (A B C): ").upper()
    if letter == 'ABC'[mydict[keyword][3]]:
        print('correct')
        correct += 1
    else:
        print('wrong')
        wrong += 1
    print('-'*30)

# final
sf = "Answers given --> {} correct and {} wrong"
print(sf.format(correct, wrong))
EarthHorse 0 Newbie Poster

Would it be possible to import tupples as the questions? Possibly by using vegaseat's code from here?
http://www.daniweb.com/software-development/python/threads/78181/starting-a-project-in-python-game-and-quiz#
And a file somewhat like the quiz.dat file?

EarthHorse 0 Newbie Poster

Would it be possible to import tupples as the questions? Possibly by using vegaseat's code from here?
http://www.daniweb.com/software-development/python/threads/78181/starting-a-project-in-python-game-and-quiz#
And a file somewhat like the quiz.dat file?

EarthHorse 0 Newbie Poster

Would it be possible to import tupples as the questions? Possibly by using vegaseat's code from here?
http://www.daniweb.com/software-development/python/threads/78181/starting-a-project-in-python-game-and-quiz#
And a file somewhat like the quiz.dat file?

EarthHorse 0 Newbie Poster

Would it be possible to import tupples as the questions? Possibly by using vegaseat's code from here?
http://www.daniweb.com/software-development/python/threads/78181/starting-a-project-in-python-game-and-quiz#
And a file somewhat like the quiz.dat file?

EarthHorse 0 Newbie Poster

Would it be possible to import tupples as the questions? Possibly by using vegaseat's code from here?
http://www.daniweb.com/software-development/python/threads/78181/starting-a-project-in-python-game-and-quiz#
And a file somewhat like the quiz.dat file?

EarthHorse 0 Newbie Poster

Would it be possible to import tupples as the questions? Possibly by using vegaseat's code from here?
http://www.daniweb.com/software-development/python/threads/78181/starting-a-project-in-python-game-and-quiz#
And a file somewhat like the quiz.dat file?

EarthHorse 0 Newbie Poster

well that's embarrassing, my computer got stuck on the human verification I didn't know it posted the same so many times until I refreshed my browser window

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Something like this will do, notice that the dictionary keys (the questions) have to be unique strings ...

import pprint

# a test list of (question, answerA, answerB, answerC, correct_letter)
# tuples
mylist = [
('Which sport uses the term LOVE?', 'Tennis', 'Golf', 'Football', 'A'),
('What is the German word for WATER?', 'Wodar', 'Wasser', 'Werkip', 'B'),
('What has eight bits?', 'Word', 'ASCII', 'Byte', 'C')
]

#pprint.pprint(mylist)  # test

# convert to question:[answerA, answerB, answerC, correct_index] dictionary
mydict = {}
for tup in mylist:
    mydict[tup[0]] = [tup[1], tup[2], tup[3], 'ABC'.index(tup[4])]

pprint.pprint(mydict)

''' result ...
{'What has eight bits?': ['Word', 'ASCII', 'Byte', 2],
 'What is the German word for WATER?': ['Wodar', 'Wasser', 'Werkip', 1],
 'Which sport uses the term LOVE?': ['Tennis', 'Golf', 'Football', 0]}
'''
EarthHorse 0 Newbie Poster

I was wondering if there is a way to have it list the questions that were incorrect.

import pprint
import random

# make string input work with Python2 or Python3
try: input = raw_input
except: pass

# a test list of (question, answerA, answerB, answerC, correct_letter)
# tuples
mylist = [
('Pick the correct resistor Color Code?', 'Black, brown, red, orange, yellow, green, blue, violet, grey, white', 
'Red, yellow, orange, blue, green, violet, grey, black, brown, white', 
"Black, brown, red orange, yellow, blue, green, violet, white, grey", 'A'),
('What is the difference between analog and digital audio?', 
'Digital is the actual representation of the signal, analog samples the signal and turns it into numbers ', 
'Analog is the actual representation of the signal, digital samples the signal and turns it into numbers', 
'Nothing they are the same', 'B'),
('What does EDID stand for?', 'External Display Information Detection', 
'Extended Digital Identification Display', 'Extended Display Identification Data', 'C'),
('What does HDMI stand for?', 'Heavy Duty Media Integration', 'High-Definition Multimedia Interface', 'High Diverse Military Intelligence', 'B'),
('Maximun number of audio channels HDMI 1.4 can pass?', '2', '4', '8', 'C'),
('What does HDCP stand for?', 'High-Definition Content Protection', 'High-bandwidth Digital Content Protection', 'High-Dynamic Creative Presentation', 'B'),
('What does dB stand for?', 'display Brackets', 'Decibel', 'digital Bits', 'B'),
('Pick the analog video connector.', 'HDMI', 'DVI', 'VGA', 'C'),
('How many channels of audio are required for 5.1 surround sound?', '5', '6', '7', 'B'),
('What is TLA', 'Three Letter Acronym', 'Technical Language Activities', 'Transponder Log Activation', 'A')
]

#pprint.pprint(mylist) # test
# convert to question:[answerA, answerB, answerC, correct_index] dictionary
mydict = {}
for tup in mylist:
    mydict[tup[0]] = [tup[1], tup[2], tup[3], 'ABC'.index(tup[4])]

#pprint.pprint(mydict)

keyword_list = list(mydict.keys())
# shuffle the keywords in place
random.shuffle(keyword_list)

print('Pick the correct answer:')
correct = 0
wrong = 0
for keyword in keyword_list:
    sf = '''\
{}
A) {}
B) {}
C) {}
'''
    print(sf.format(keyword, mydict[keyword][0],
                    mydict[keyword][1], mydict[keyword][2]))
    letter = input("Enter letter of your choice (A B C): ").upper()
    if letter == 'ABC'[mydict[keyword][3]]:
        print('correct')
        correct += 1
    else:
        print('wrong')
        wrong += 1
    print('-' * 30)

# final
sf = "You got --> {} correct and {} wrong"
print(sf.format(correct, wrong))


   #How could this work, I have seen a similar way in other posts but can't figure out how to adapt for this code?
    for q in wrong:
        print (q)
Doug_2 0 Newbie Poster

I have created a text file that list forenames and surnames (separated with a comma)

My program display the question and possible answers but does not recognise the correct answer. Also, an error occurs if the last file item is chosen because it can't handle num2 = num1 +1

import random

print ('Welcome to the name quiz')

play = input('Are you ready to play?  (Y or N): ')

if play == 'Y':

    with open ('lads.txt','r') as f:
        question = [line.split(',') for line in f]
        i = 0
        score = 0
        while i <len(question):
            num1 = random.randint(0,len(question)-1)
            num2 = num1 +1
            num3 = num1 -1
            print("\nQuestion number ",i+1,": \nWhat is the surname for ", question[num1][0], "?")
            print ()
            print ('A',question[num1][1])
            print ('B',question[num2][1])
            print ('C',question[num3][1])
            print()
            answer = input('Answer: ')
            if (answer == str(question[num1][1])):
                print ('Good answer')
                score +=1
            else:
                print ('Wrong answer - the correct answer was',question[num1][1])
            i +=1
        print ('Your score is:',score)
else:
    print ('Thanks for looking')

Editor's Note:
Please start your own thread on this problem rather than taking it on to an existing thread. Chances that someone can help are much higher.

Chance_1 0 Newbie Poster

Very nice tutorial. Thanks so much for sharing; Jesus Christ Bless! :)

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.