Reputation: 27
I took a step back from programming, and forgot nearly everything I ever learned (not that it was much, anyhow). I wanted to refresh, with something relatively simple, so I've been trying to create a practice test that I plan on expanding into flash cards with Tkinter. It's a study aid for my CDC's in the Air Force, so I won't post the actual contents of my dictionary, but here's the code (I got it from YouTube). I want to be able to remove (pop?) correct answers from the dictionary, so they don't come up anymore, and keep the wrong answers in the loop of questions. I'd also like to add in something to monitor the sections that I need to brush up on. Currently it just loops through all of the questions and then displays the amount of correct/wrong. It works, but it's not as effective as I'd like to to be. I've been searching for ways to do this but I can't seem to find a concrete answer and I had success with a post here in the past, so I figured I'd give it a shot. You guys are impressive, and I appreciate any responses and feedback I can get. I have 63 questions so my code is really long, and I was thinking it would be better to put my dictionary into a separate file and just import that file?
The numbers indicate the section it is pertaining to. I want to be able to grab the numbers (002) of all the wrong answers, and print them after I print # of correct/wrong answers. So it would say 10 correct, 20 wrong, 5 from section 002, 15 from section 3, or whatever.
import random
try: input = raw_input
except: pass
mydict = {
'(001) Insert Question Here' : ['Answer A Text', 'Answer B Text', 'Answer C Text', 'Answer D Text', 0],
'(001) Insert Question Here' : ['Answer A Text', 'Answer B Text', 'Answer C Text', 'Answer D Text', 3],
'(002) Insert Question Here' : ['Answer A Text', 'Answer B Text', 'Answer C Text', 'Answer D Text', 2],
'(002) Insert Question Here' : ['Answer A Text', 'Answer B Text', 'Answer C Text', 'Answer D Text', 0]
}
keyword_list = list(mydict.keys())
random.shuffle(keyword_list)
print('Pick the correct answer: ')
correct = 0
wrong = 0
buffer = '-'*30
next_question = "Next Question: "
for keyword in keyword_list:
sf = '''
{}
A) {}
B) {}
C) {}
D) {}
'''
print(sf.format(keyword, mydict[keyword][0],
mydict[keyword][1], mydict[keyword][2], mydict[keyword][3]))
letter = input("Enter Letter Of Your Choice (A, B, C, D): ").upper()
if letter == "ABCD"[mydict[keyword][4]]:
correct += 1
print('Correct! Great Job!')
print(buffer)
print(next_question)
else:
wrong += 1
print('Wrong :(')
print('The correct answer was ' + 'ABCD'[mydict[keyword][4]])
print(buffer)
print(next_question)
sf = "Answers given ---> {} correct and {} wrong"
print(sf.format(correct, wrong))
Upvotes: 1
Views: 44
Reputation: 166
Here's your code, slightly reworked and with your ideas implemented:
import random
try: input = raw_input
except: pass
mydict = {
'(001) Question A' : ['Answer A Text', 'Answer B Text', 'Answer C Text', 'Answer D Text', 0],
'(001) Question B' : ['Answer A Text', 'Answer B Text', 'Answer C Text', 'Answer D Text', 3],
'(002) Question A' : ['Answer A Text', 'Answer B Text', 'Answer C Text', 'Answer D Text', 2],
'(002) Question B' : ['Answer A Text', 'Answer B Text', 'Answer C Text', 'Answer D Text', 0]
}
results = {} # Dictionary for storing question results per section.
key_list = list(mydict.keys())
random.shuffle(key_list)
correct = 0
wrong = 0
buffer = '-'*30
sf = "{}\nA) {}\nB) {}\nC) {}\nD) {}"
remaining = len(key_list) # Remaining questions
while remaining: # While questions remain.
# Get a random key.
key = random.choice(key_list)
print('{} questions remaining. '.format(remaining))
print(sf.format(key, mydict[key][0], mydict[key][1], mydict[key][2], mydict[key][3]))
letter = input("Enter Letter Of Your Choice (A, B, C, D): ").upper()
if letter == "ABCD"[mydict[key][4]]:
correct += 1
print('Correct! Great Job!')
key_list.remove(key) # Remove correctly answered questions from circulation.
remaining -= 1 # Decrement remaining questions.
# Update results.
section = key[: 6]
if section not in results:
results[section] = [0, 0]
results[section][0] += 1
input("...")
print(buffer)
else:
wrong += 1
print('Wrong :(')
print('The correct answer was ' + 'ABCD'[mydict[key][4]])
# Update results.
section = key[: 6]
if section not in results:
results[section] = [0, 0]
results[section][1] += 1
input("...")
print(buffer)
sf = "Answers given ---> {} correct and {} wrong"
print(sf.format(correct, wrong))
for section, result in results.items():
print("{}: {} correct, {} wrong.".format(section, result[0], result[1]))
You might get the same question more than once in a row. Here, a section
is the first 5 characters of a question (key[: 5]
).
Upvotes: 1