Question: I am working with Python I am almost completed with my assignment and I noticed I was supposed to randomize the output to the user.
I am working with Python
I am almost completed with my assignment and I noticed I was supposed to randomize the output to the user. However everything I tried doesn't seem to work and I don't know how to go about randomizing it properly. I did get it to randomize kind of but the inputs were not matching up to the outputs. I did some testing and I found while the states were randomizing like I wanted it wasn't matching the correct capitals to the states. It was still taking user input of the capitals going down the dictionary in order.
so it was doing:
What is the capital of Kentucky? (or enter 0 to quit):
Frankfort
incorrect
-
Its obviously correct I instead put Montgomery which of course is first in the dictionary and it said correct.
which is wrong!
I reverted my work back to before I played around with it. I just need to know how to randomize it where it still also stays matches up to the correct input based off the dictionary.
This is my work so far as well as the instructions for the assignment:
_____________________________________________________
Write a program that
Creates a dictionary containing the U.S. states as keys, and their capitals as values.
Use the Internet to get a list of the states and their capitals.
As an alternative to the U.S. states, the program can use the names of countries and their capitals.
The program should randomly quiz the user by displaying the name of a state and asking the user to enter that states capital.
The program should compare the user's input to the random state and keep a running count of the number of correct and incorrect responses.
The code should loop continually until the user decides to quit.
To randomly pick a state from the dictionary you should use the "random.choice()" method found in the random module. You will need to add the import random statement to the top of you code.
The choice(some-list) method takes a "list" as a parameter, and then returns a random item from the provided list. NOTE: If you attempt to use the "keys()" method of a dictionary object to return all of the keys for the random.choice() function you will get an error. The returned object for the .keys() method is actually a 'view' instead of list. In Python 3+ you can use the list() function instead, which will extract the dictionary keys as a list.
example: current_country = random.choice(list(countries))
Programming Steps (Pseudo-Code) - Follow these steps to accomplished the finished project:
Initialize the dictionary and also two integer variables to act as accumulators for correct and incorrect responses to the quiz.
Create a loop that will continue until the user gives the proper command to end the quiz
Inside the loop randomly pick a state from the dictionary and store it as the current state for the quiz
Ask the user to provide the correct capital for the state.
import random correct_answers = 0 wrong_answers = 0 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", "West Virginia": "Charleston", "Wisconsin": "Madison", "Wyoming": "Cheyenne"} random.choice(list(capitals.items()))
for s in capitals.keys(): state = input(str("What is the capital of " + s + "? (or enter 0 to quit): ")) if state == "0": print("You had", correct_answers, "correct responses and", wrong_answers, "incorrect responses.") break if state.upper() == capitals[s].upper(): correct_answers += 1 print("Correct!") else: wrong_answers += 1 print("Sorry, that's incorrect.") choice = input("Do you want to play again y/n: ") if choice == "n": print("You had", correct_answers, "correct responses and", wrong_answers, "incorrect responses.") break while choice not in ("y", "n"): print("Invalid choice. Please try again.") choice = input("Do you want to play again y/n: ") if choice == "n": print("You had", correct_answers, "correct responses and", wrong_answers, "incorrect responses.") break
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
