Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- import random
- Capitals={'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix','Arkansas': 'Little Rock',
- 'California': 'Sacramento', 'Colorado': 'Denver','Connecticut': 'Hartford', 'Delaware': 'Dover',
- 'Florida': 'Tallahassee','Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise',
- 'Illinois':'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas':'Topeka',
- 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine':'Augusta', 'Maryland': 'Annapolis',
- 'Massachusetts': 'Boston', 'Michigan':'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson',
- 'Missouri':'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada':'Carson City',
- 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New Mexico': 'Santa Fe', 'New York': 'Albany',
- 'North Carolina': 'Raleigh','North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City',
- 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence','South Carolina': 'Columbia',
- 'South Dakota': 'Pierre', 'Tennessee':'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City',
- 'Vermont':'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'WestVirginia': 'Charleston',
- 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}
- IndexedCapitals = Capitals.copy() #creates a Copy of the dictionary Capitals
- IndexedCapitals.update([(v,k) for k, v in IndexedCapitals.items()]) #creates an inverse dictionary on top of the original so you can pull states as values and not keys. important for Line 38
- states = list(Capitals.keys()) #creates a list of states so they can be indexed
- cities = list(Capitals.values()) #creates a list of cities so they can be indexed
- random.shuffle(states) #randomly shuffles the states from the Capitals Dictionary
- random.shuffle(cities)
- answerindex=["A","B","C","D"] #creates an indexed list of Multiple choice selection to reference
- for quiznum in range(10): #loops through X number of times
- quizfile=open('C:\\Users\\Nassim.Rostane\\MyScripts\\QuizExercise\\quiz'+str(quiznum)+'.txt','w') #creates a text file named quiz(X) X is the number of the loop above.
- workingstates=states.copy() #creates a list of states for removing values so that the may not be repeated.
- workingcities=cities.copy()
- for QuestionNum in range(1,11): #loops through 10 times the below code
- setcity = workingcities[random.randint(0 , (len(workingcities) - 1))] #randomly generates a capital city for the question and assigns it to set city.
- workingcities.remove(setcity) #removes the Set city from above so that way it does not have a chance to be repeated
- quizfile.write(str(QuestionNum) + ")" + ' please select the correct State for the capital city of: '+str(setcity) +"." + "\n") #writes the question using the city it randomly generated from Set City
- statecheck=[] #creates a new list for the purposes of storing randomly generated states. important for line 38.
- for potentialstates in range(1,4): #loops through the below 3 times
- randomstate=workingstates[random.randint(0,(len(workingstates)-1))] #randomly generates a state from the state list in line 27
- workingstates.remove(randomstate) #removes the randomly generated state from above out of the ist from line 27
- statecheck.append(randomstate) #adds the randomly generated state to the list statecheck from line 33
- if IndexedCapitals.get(setcity) in statecheck: #checks if the randomly generated states from above where the right answer to the SetCity from line30
- statecheck.append(workingstates[random.randint(0,(len(workingstates)-1))]) #if the right answer is in the list statecheck that was randomely generated this will add another random state for a total of 4 choices
- else:
- statecheck.append(IndexedCapitals.get(setcity)) #if the right answer is not in the randomly generated statecheck it will add it as the 4th option.
- random.shuffle(statecheck) #this will shuffle the order of the statecheck list so the right answer wont always be #4
- for potentialanswers in range(0,4): #loops through the below code 4 times.
- quizfile.write(" "+str(answerindex[potentialanswers])+")"+" "+str(statecheck[potentialanswers]+"\n")) #writes out the answers in the statecheck that contain 3 random states and a correct state.
- quizfile.write("\n")
- workingstates=states.copy() #reappends all the deleted states from line 31 so next question has all the states to work with.
- quizfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement