forked from iliyahoo/Automate-The-Boring-Stuff-With-Python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandomQuizGenerator.py
More file actions
executable file
·44 lines (36 loc) · 2.76 KB
/
randomQuizGenerator.py
File metadata and controls
executable file
·44 lines (36 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# randomQuizGenerator. py - Creates quizzes with questions and answers in
# random order, along with the answer key.
import random
pupil = 3
# The quiz data . Keys are states and values are their capitals .
capitals = {'Alabama':'Montgomery','Alaska':'Juneau','Arizona':'Phoenix','Arkansas':'LittleRock','California':'Sacramento','Colorado':'Denver','Connecticut':'Hartford','Delaware':'Dover','Florida':'Tallahassee','Georgia':'Atlanta','Hawaii':'Honolulu','Idaho':'Boise','Illinois':'Springfield','Indiana':'Indianapolis','Iowa':'DesMoines','Kansas':'Topeka','Kentucky':'Frankfort','Louisiana':'BatonRouge','Maine':'Augusta','Maryland':'Annapolis','Massachusetts':'Boston','Michigan':'Lansing','Minnesota':'SaintPaul','Mississippi':'Jackson','Missouri':'JeffersonCity','Montana':'Helena','Nebraska':'Lincoln','Nevada':'CarsonCity','NewHampshire':'Concord','NewJersey':'Trenton','NewMexico':'SantaFe','NewYork':'Albany','NorthCarolina':'Raleigh','NorthDakota':'Bismarck','Ohio':'Columbus','Oklahoma':'OklahomaCity','Oregon':'Salem','Pennsylvania':'Harrisburg','RhodeIsland':'Providence','SouthCarolina':'Columbia','SouthDakota':'Pierre','Tennessee':'Nashville','Texas':'Austin','Utah':'SaltLakeCity','Vermont':'Montpelier','Virginia':'Richmond','Washington':'Olympia','WestVirginia':'Charleston','Wisconsin':'Madison','Wyoming':'Cheyenne'}
# Generate quiz files .
for quizNum in range(pupil):
# Create the quiz and answer key files .
quizFile = open('capitalsquiz%d.txt' % (quizNum + 1), 'w')
answerKeyFile = open('capitalsquiz_answers%d.txt' % (quizNum + 1), 'w')
# write out the header for the quiz.
quizFile.write('Name:\n\nDate:\n\nPeriod:\n\n')
quizFile.write(' ' * 20 + 'State capital quiz (Form %d)' % (quizNum + 1))
quizFile.write('\n\n')
# Shuffle the order of the states .
states = list(capitals.keys())
random.shuffle(states)
# Loop through all states, making a question for each .
for questionNum in range(len(capitals)):
correctAnswer = capitals[states[questionNum]]
wrongAnswers = list(capitals.values())
del wrongAnswers[wrongAnswers.index(correctAnswer)]
wrongAnswers = random.sample(wrongAnswers, 3)
answerOptions = wrongAnswers + [correctAnswer]
random.shuffle(answerOptions)
# Write the question and the answer options to the quiz file.
quizFile.write('%d. What is the capital of %s?\n' % (questionNum + 1, states[questionNum]))
for i in range(len('ABCD')):
quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions[i]))
quizFile.write('\n')
# Write the answer key to a file.
answerKeyFile.write('%d. %s\n' % (questionNum + 1, 'ABCD'[answerOptions.index(correctAnswer)]))
quizFile.close()
answerKeyFile.close()