[READ-ONLY] Mirror of https://github.com/mrgnw/python-scripts.
3.4 kB
106 lines
1# quiz.py
2# A simple command-line quiz
3# LearnPythonTheHardWay.org recommended making flash cards to memorize the items in dictionary
4# Since I was just learning Pythong, I decided to use Python to learn Python
5 # by making this quiz
6
7import random
8
9# What python gets when it asks for a term that's not in the dictionary
10default = 'WRONG'
11
12# Determines if the loop continues to run.
13contin = "y"
14
15# commands to enter at any time to exit quiz or skip question
16# could create interpretAnswer function to accept multiple parameters? Nah
17exitKeys = ["x", "exit"]
18skipKeys = ["", "skip"]
19flipKeys = ["flip", "\\"]
20commandList = exitKeys + skipKeys + flipKeys
21
22# dictionary of escape sequences
23dictionary = {
24 "\\\\" : "backslash",
25 "\\'" : "apostrophe",
26 "\\\"" : "quote",
27 "\\a" : "ASCII bell",
28 "\\b" : "backspace",
29 "\\f" : "formfeed",
30 "\\n" : "linefeed",
31 "\\r" : "carriage return",
32 "\\t" : "tab",
33 "\\v" : "vertical tab"
34}
35
36def displayPercent(good, bad):
37 if good+bad == 0 : return 0
38 else : return "{:.0%}".format(good/(good+bad))
39
40def displayFinalScore(good, bad):
41 print "\n","SCORE"
42 print displayPercent(right, wrong)
43 print "%d/%d" % (right, right+wrong)
44
45# Asks a question
46def askTerm(dictionary):
47 qToAsk = random.choice(dictionary.keys())
48 print dictionary[qToAsk],
49
50 response = raw_input("\t")
51
52 # Respons is a command from commandList, not an attempt at an answer
53 if response.lower() in commandList :
54 return response.lower()
55 # RIGHT ANSWER:
56 elif dictionary.get(qToAsk, default) == dictionary.get(response, default) :
57 return response
58 else :
59 print " \tAnswer: %s" % qToAsk
60 return "WRONG"
61
62# Asks for the value instead of key
63# Returns copy of dictionary with flipped keys and values
64def flipDictionary(dictionary):
65 return dict((dictionary[k], k) for k in dictionary)
66
67right = 0.0
68wrong = 0.0
69
70# creates a flipped version of dictionary dictionary
71# (makes values into keys, keys into values)
72flippy = flipDictionary(dictionary)
73quizDictionary = dictionary
74
75qnum = 0
76while contin.lower() == "y" or contin.lower() == "yes":
77 qnum += 1
78 print qnum,
79
80 new_answer = askTerm(quizDictionary)
81
82 if new_answer.lower() in exitKeys :
83 contin = "n"
84 displayFinalScore(right, wrong)
85
86 # Switch between term view and definition view at any prompt
87 if new_answer in flipKeys :
88 newDictionary = flipDictionary(quizDictionary)
89 quizDictionary = newDictionary
90 print "\tFlipping..."
91
92 if new_answer in quizDictionary :
93 if len(quizDictionary) > 1 :
94 quizDictionary.pop(new_answer)
95 right += 1
96 print " +1\t%s (%d/%d)" % (displayPercent(right,wrong), right, right+wrong)
97 else :
98 contin = "n"
99 print "\nYay, you're done!"
100 displayFinalScore(right, wrong)
101
102 elif new_answer == "WRONG" :
103 wrong += 1
104 print " 1-\t%s (%d/%d)" % (displayPercent(right,wrong), right, right+wrong)
105
106else : print ""