What is a straight forward approach to matching and enumerating the results of a quiz using Python
In the code below (which shows all my neophyteness in Python), I took the books of the KJV Bible and put them in logical order via a list and dictionary. I then used a random method to display 5 random books to a user along with the position of the each book in a random list for the user to match (something for me to better learn the books of the Bible). My difficulty is figuring out how to go about saving the results and tallying up the number correct/wrong. I also would like to give the user an option to choose the number of questions like 10, 15, 20, or 30 .... Can someone give me direction on this? Thanks!
import random
import os
if __name__=='__main__':
books=['Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy', 'Joshua',
'Judges', 'Ruth', 'I Samuel', 'II Samuel', 'I Kings', 'II Kings',
'I Chronicles', 'II Chronicles', 'Ezra', 'Nehemiah', 'Esther', 'Job', 'Psalms',
'Proverbs', 'Ecclesiastes', 'Song of Solomon', 'Isaiah', 'Jeremiah',
'Lamentations', 'Ezekiel', 'Daniel', 'Hosea', 'Joel', 'Amos', 'Obadiah',
'Jonah', 'Micah', 'Nahum', 'Habakkuk', 'Zephaniah', 'Haggai', 'Zechariah',
'Malachi', 'Matthew', 'Mark', 'Luke', 'John', 'Acts', 'Romans', 'I Corinthians',
'II Corinthians', 'Galatians', 'Ephesians', 'Philippians',
'Colossians', 'I Thessalonians', 'II Thessalonians', 'I Timothy', 'II Timothy',
'Titus', 'Philemon', 'Hebrews', 'James', 'I Peter', 'II Peter', 'I John',
'II John', 'III John', 'Jude', 'Revelation']
Title = "{0:^78}".format("Welcome to the Bible book quiz!\n\n")
seleCtion = raw_input(" The Bible has a number of " + str(len(books)) + " books.\n Select Next to see them below:\n" + "{0:^78}".format("[1]Next [2]Exit\n") )
if seleCtion == '1':
#This section displays the books of the Bible and their indexes.
count = 1
indexMap = {}
for i, bname in enumerate(books):
print '\n{0:3d}. {1}'.format(count, bname)
indexMap[count] = i
count +=1
elif seleCtion == '2':
print Title
print "\n Let's start the quiz:\n\n\n"
else:
print 'You must select 1 or 2'
mydict_book = {'Genesis':1, 'Exodus':2, 'Leviticus':3, 'Numbers':4, 'Deuteronomy':5, 'Joshua':6,
'Judges':7, 'Ruth':8, 'I Samuel':9, 'II Samuel':10, 'I Kings':11, 'II Kings':12,
'I Chronicles':13, 'II Chronicles':14, 'Ezra':15, 'Nehemiah':16, 'Esther':17, 'Job':18, 'Psalms':19,
'Proverbs':20, 'Ecclesiastes':21, 'Song of Solomon':22, 'Isaiah':23, 'Jeremiah':24,
'Lamentations':25, 'Ezekiel':26, 'Daniel':27, 'Hosea':28, 'Joel':29, 'Amos':30, 'Obadiah':31,
'Jonah':32, 'Micah':33, 'Nahum':34, 'Habakkuk':35, 'Zephaniah':36, 'Haggai':37, 'Zechariah':38,
'Malachi':39, 'Matthew':40, 'Mark':41, 'Luke':42, 'John':43, 'Acts':44, 'Romans':45, 'I Corinthians':46,
'II Corinthians':47, 'Galatians':48, 'Ephesians':49, 'Philippians':50,
'Colossians':51, 'I Thessalonians':52, 'II Thessalonians':53, 'I Timothy':54, 'II Timothy':55,
'Titus':56, 'Philemon':57, 'Hebrews':58, 'James':59, 'I Peter':60, 'II Peter':61, 'I John':62,
'II John':63, 'III John':64, 'Jude':65, 'Revelation':66}
#new_dict = dict.fromkeys(books, counter)
#print new_dict
while 1:
try:
#This section starts the random book selection index match
user_sel = []
print '\n\n\n H开发者_运维百科ere are the first 5 books in the quiz: \n'
sampler =random.sample(books, 5)
first = str(sampler[0])
second = str(sampler[1])
third = str(sampler[2])
fourth = str(sampler[3])
fifth = str(sampler[4])
user_sel = mydict_book[first], mydict_book[second], mydict_book[third], mydict_book[fourth], mydict_book[fifth]
num_sampler = random.sample(user_sel, 5)
print sampler
print '\nMatch the correct numeric position below:'
print '\n', num_sampler
samp1 = int(raw_input('\nWhich number is ' + sampler[0] +': '))
samp2 = int(raw_input('Which number is ' + sampler[1] +': '))
samp3 = int(raw_input('Which number is ' + sampler[2] +': '))
samp4 = int(raw_input('Which number is ' + sampler[3] +': '))
samp5 = int(raw_input('Which number is ' + sampler[4] +': '))
# taking the the users answer and finding the resultant book
# need to put an if statement condition for !< 1 and ! < 66
answer1=books[samp1-1]
answer2=books[samp2-1]
answer3=books[samp3-1]
answer4=books[samp4-1]
answer5=books[samp5-1]
# taking the book and finding the numeric value associated with it
right1 = mydict_book[answer1]
right2 = mydict_book[answer2]
right3 = mydict_book[answer3]
right4 = mydict_book[answer4]
right5 = mydict_book[answer5]
#display what my answers yield
print '\nYour Answers yield:\n'
print "1. " + str(answer1)
print "2. " + str(answer2)
print "3. " + str(answer3)
print "4. " + str(answer4)
print "5. " + str(answer5)
#takes the random books converts them to strings
first = str(sampler[0])
second = str(sampler[1])
third = str(sampler[2])
fourth = str(sampler[3])
fifth = str(sampler[4])
# print the numeric value and string value of the correct answers.
print "\nThe Correct Answers are:\n"
print sampler[0] + " - " , mydict_book[first], tstmnt
print sampler[1] + " - " , mydict_book[second], tstmnt
print sampler[2] + " - " , mydict_book[third], tstmnt
print sampler[3] + " - " , mydict_book[fourth], tstmnt
print sampler[4] + " - " , mydict_book[fifth], tstmnt
continue
except ValueError:
break
import random
import os
if __name__=='__main__':
books = ['Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy',
'Joshua', 'Judges', 'Ruth', 'I Samuel', 'II Samuel', 'I Kings',
'II Kings', 'I Chronicles', 'II Chronicles', 'Ezra', 'Nehemiah',
'Esther', 'Job', 'Psalms', 'Proverbs', 'Ecclesiastes',
'Song of Solomon', 'Isaiah', 'Jeremiah', 'Lamentations',
'Ezekiel', 'Daniel', 'Hosea', 'Joel', 'Amos', 'Obadiah', 'Jonah',
'Micah', 'Nahum', 'Habakkuk', 'Zephaniah', 'Haggai', 'Zechariah',
'Malachi', 'Matthew', 'Mark', 'Luke', 'John', 'Acts', 'Romans',
'I Corinthians', 'II Corinthians', 'Galatians', 'Ephesians',
'Philippians', 'Colossians', 'I Thessalonians',
'II Thessalonians', 'I Timothy', 'II Timothy', 'Titus',
'Philemon', 'Hebrews', 'James', 'I Peter', 'II Peter', 'I John',
'II John', 'III John', 'Jude', 'Revelation']
mydict_book = dict((bname,i) for i,bname in enumerate(books,start=1))
while True:
seleCtion = raw_input(" The Bible has a number of %s books.\n" % len(books) \
+ "Select Next to see them below:\n" \
+ "{0:^78}".format("[1]Print All books [2]Start Quiz\n"))
if seleCtion == '1':
print '\n'.join('{0:3d}. {1}'.format(i, bname) for i,bname in mydict_book.iteritems())
break
elif seleCtion == '2':
print "{0:^78}\nLet's start the quiz:\n\n".format("Welcome to the Bible book quiz!\n\n")
break
else:
print 'You must select 1 or 2'
while 1:
try:
num_ques = int(raw_input('\nEnter the number of questions you wanna be asked: '))
sampler = random.sample(books, num_ques)
if not sampler: exit(0)
num_sampler = map(lambda bname: mydict_book[bname] , sampler)
print 'num_sampler==',num_sampler
print ('\n\n\nHere are the first %s books in the quiz: \n%s'
'\nMatch the correct numeric positions below :\n%s') \
% (num_ques, str(sampler)[1:-1], str(random.sample(num_sampler,num_ques))[1:-1])
samp = [ int(raw_input('\nWhich number is %s : ' % books[i-1])) for i in num_sampler ]
your = zip(samp,sampler)
your.sort()
print '\nYour Answers yield:\n' + '\n'.join('%2s. %s' % (n,bk) for n,bk in your)
correct = zip(num_sampler,sampler)
correct.sort()
print "\nThe Correct Answers are:\n" + '\n'.join('%2s - %s' % (n,bk) for n,bk in correct)
continue
except ValueError:
break
One thing that might help you is to use lists, list comprehension statements and loops, e.g.
q_count = int(raw_input('How many questions per quiz?'))
quiz_items = [str(sampler(x)) for x in range(q_count)]
user_sel = [mydict_book[x] for x in quiz_items]
...
answers = []
for i in range(q_count):
next_answer = int(raw_input('\nWhich number is ' + sampler[i] +': '))
answers.append(books[next_answer-1])
...
"My difficulty is figuring out how to go about saving the results and tallying up the number correct/wrong. I also would like to give the user an option to choose the number of questions like 10, 15, 20, or 30 .... Can someone give me direction on this?:
I guess you can do something like:
right = 0
wrong = 0
...
numSamp = raw_input("Enter the amount of samples: ")
for i in xrange(numSamp):
answer = int(raw_input('\nWhich number is ' + sampler[i] +': '))
if book[answer-1] == sampler[i]:
right=right+1
else:
wrong=wrong+1
This is the complete code, execute it and see if you like it. I've erased some of your lines.
import random
import os
if __name__=='__main__':
books=['Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy', 'Joshua',
'Judges', 'Ruth', 'I Samuel', 'II Samuel', 'I Kings', 'II Kings',
'I Chronicles', 'II Chronicles', 'Ezra', 'Nehemiah', 'Esther', 'Job', 'Psalms',
'Proverbs', 'Ecclesiastes', 'Song of Solomon', 'Isaiah', 'Jeremiah',
'Lamentations', 'Ezekiel', 'Daniel', 'Hosea', 'Joel', 'Amos', 'Obadiah',
'Jonah', 'Micah', 'Nahum', 'Habakkuk', 'Zephaniah', 'Haggai', 'Zechariah',
'Malachi', 'Matthew', 'Mark', 'Luke', 'John', 'Acts', 'Romans', 'I Corinthians',
'II Corinthians', 'Galatians', 'Ephesians', 'Philippians',
'Colossians', 'I Thessalonians', 'II Thessalonians', 'I Timothy', 'II Timothy',
'Titus', 'Philemon', 'Hebrews', 'James', 'I Peter', 'II Peter', 'I John',
'II John', 'III John', 'Jude', 'Revelation']
Title = "{0:^78}".format("Welcome to the Bible book quiz!\n\n")
seleCtion = raw_input(" The Bible has a number of " + str(len(books)) + " books.\n Select Next to see them below:\n" + "{0:^78}".format("[1]Print All books [2]Start Quiz\n") )
if seleCtion == '1':
#This section displays the books of the Bible and their indexes.
count = 1
indexMap = {}
for i, bname in enumerate(books):
print '\n{0:3d}. {1}'.format(count, bname)
indexMap[count] = i
count +=1
elif seleCtion == '2':
print Title
print "\n Let's start the quiz:\n\n\n"
else:
print 'You must select 1 or 2'
mydict_book = {'Genesis':1, 'Exodus':2, 'Leviticus':3, 'Numbers':4, 'Deuteronomy':5, 'Joshua':6,
'Judges':7, 'Ruth':8, 'I Samuel':9, 'II Samuel':10, 'I Kings':11, 'II Kings':12,
'I Chronicles':13, 'II Chronicles':14, 'Ezra':15, 'Nehemiah':16, 'Esther':17, 'Job':18, 'Psalms':19,
'Proverbs':20, 'Ecclesiastes':21, 'Song of Solomon':22, 'Isaiah':23, 'Jeremiah':24,
'Lamentations':25, 'Ezekiel':26, 'Daniel':27, 'Hosea':28, 'Joel':29, 'Amos':30, 'Obadiah':31,
'Jonah':32, 'Micah':33, 'Nahum':34, 'Habakkuk':35, 'Zephaniah':36, 'Haggai':37, 'Zechariah':38,
'Malachi':39, 'Matthew':40, 'Mark':41, 'Luke':42, 'John':43, 'Acts':44, 'Romans':45, 'I Corinthians':46,
'II Corinthians':47, 'Galatians':48, 'Ephesians':49, 'Philippians':50,
'Colossians':51, 'I Thessalonians':52, 'II Thessalonians':53, 'I Timothy':54, 'II Timothy':55,
'Titus':56, 'Philemon':57, 'Hebrews':58, 'James':59, 'I Peter':60, 'II Peter':61, 'I John':62,
'II John':63, 'III John':64, 'Jude':65, 'Revelation':66}
#new_dict = dict.fromkeys(books, counter)
#print new_dict
while 1:
try:
#This section starts the random book selection index match
user_sel = []
num_ques = int(raw_input('Enter the number of questions you wanna be asked: '))
if num_ques == 0:
exit(0)
sampler =random.sample(books, num_ques)
print '\n\n\n Here are the first ' + str(num_ques) + ' books in the quiz: \n'
in_string = [] # this is your 'first', 'second', and so on...
for i in range(num_ques):
in_string.append( str(sampler[i]) )
#user_sel = mydict_book[first], mydict_book[second], mydict_book[third], mydict_book[fourth], mydict_book[fifth]
for i in range(num_ques):
user_sel.append( mydict_book[in_string[i]] )
num_sampler = random.sample(user_sel, num_ques)
print sampler
print '\nMatch the correct numeric position below:'
print '\n', num_sampler
#samp1 = int(raw_input('\nWhich number is ' + sampler[0] +': '))
samp = []
for i in range(num_ques):
samp.append( int(raw_input('\nWhich number is ' + sampler[i] + ': ') ) )
# taking the the users answer and finding the resultant book
# need to put an if statement condition for !< 1 and ! < 66
#answer1=books[samp1-1]
answers = []
for i in range(num_ques):
answers.append( books[samp[i]-1] )
# taking the book and finding the numeric value associated with it
right = []
for i in range(num_ques):
right.append( mydict_book[ answers[i] ] )
#right1 = mydict_book[answer1]
#display what my answers yield
print '\nYour Answers yield:\n'
#print "1. " + str(answer1)
for i in range(num_ques):
print str(i+1) + '. ' + str(answers[i])
#takes the random books converts them to strings
positions = []
for i in range(num_ques):
positions.append( str(sampler[i]) )
#first = str(sampler[0])
#second = str(sampler[1])
#third = str(sampler[2])
#fourth = str(sampler[3])
#fifth = str(sampler[4])
# print the numeric value and string value of the correct answers.
print "\nThe Correct Answers are:\n"
for i in range(num_ques):
print sampler[i] + ' - ', mydict_book[ positions[i] ]
#print sampler[0] + " - " , mydict_book[first], tstmnt
#print sampler[1] + " - " , mydict_book[second], tstmnt
#print sampler[2] + " - " , mydict_book[third], tstmnt
#print sampler[3] + " - " , mydict_book[fourth], tstmnt
#print sampler[4] + " - " , mydict_book[fifth], tstmnt
continue
except ValueError:
break
精彩评论