Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. #! python3
  2. import random
  3. Capitals={'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix','Arkansas': 'Little Rock',
  4. 'California': 'Sacramento', 'Colorado': 'Denver','Connecticut': 'Hartford', 'Delaware': 'Dover',
  5. 'Florida': 'Tallahassee','Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise',
  6. 'Illinois':'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas':'Topeka',
  7. 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine':'Augusta', 'Maryland': 'Annapolis',
  8. 'Massachusetts': 'Boston', 'Michigan':'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson',
  9. 'Missouri':'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada':'Carson City',
  10. 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New Mexico': 'Santa Fe', 'New York': 'Albany',
  11. 'North Carolina': 'Raleigh','North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City',
  12. 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence','South Carolina': 'Columbia',
  13. 'South Dakota': 'Pierre', 'Tennessee':'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City',
  14. 'Vermont':'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'WestVirginia': 'Charleston',
  15. 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}
  16. IndexedCapitals = Capitals.copy() #creates a Copy of the dictionary Capitals
  17. 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
  18.  
  19. states = list(Capitals.keys()) #creates a list of states so they can be indexed
  20. cities = list(Capitals.values()) #creates a list of cities so they can be indexed
  21. random.shuffle(states) #randomly shuffles the states from the Capitals Dictionary
  22. random.shuffle(cities)
  23. answerindex=["A","B","C","D"] #creates an indexed list of Multiple choice selection to reference
  24.  
  25. for quiznum in range(10): #loops through X number of times
  26. 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.
  27. workingstates=states.copy() #creates a list of states for removing values so that the may not be repeated.
  28. workingcities=cities.copy()
  29. for QuestionNum in range(1,11): #loops through 10 times the below code
  30. setcity = workingcities[random.randint(0 , (len(workingcities) - 1))] #randomly generates a capital city for the question and assigns it to set city.
  31. workingcities.remove(setcity) #removes the Set city from above so that way it does not have a chance to be repeated
  32. 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
  33. statecheck=[] #creates a new list for the purposes of storing randomly generated states. important for line 38.
  34. for potentialstates in range(1,4): #loops through the below 3 times
  35. randomstate=workingstates[random.randint(0,(len(workingstates)-1))] #randomly generates a state from the state list in line 27
  36. workingstates.remove(randomstate) #removes the randomly generated state from above out of the ist from line 27
  37. statecheck.append(randomstate) #adds the randomly generated state to the list statecheck from line 33
  38. if IndexedCapitals.get(setcity) in statecheck: #checks if the randomly generated states from above where the right answer to the SetCity from line30
  39. 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
  40. else:
  41. statecheck.append(IndexedCapitals.get(setcity)) #if the right answer is not in the randomly generated statecheck it will add it as the 4th option.
  42. random.shuffle(statecheck) #this will shuffle the order of the statecheck list so the right answer wont always be #4
  43. for potentialanswers in range(0,4): #loops through the below code 4 times.
  44. 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.
  45. quizfile.write("\n")
  46. workingstates=states.copy() #reappends all the deleted states from line 31 so next question has all the states to work with.
  47. quizfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement