Question

Create a program that solves programming exercise below. Within this folder is the file state_capitals.txt. The...

Create a program that solves programming exercise below. Within this folder is the file state_capitals.txt. The file is organized with a state name on a line and the next line has its capital. Your program should read this in and create a dictionary from this file. Then use this dictionary to create a quiz, ask the user for 5 random choice states and give the results back at end.

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.) ALREADY INSIDE state_capitals.txt. 
The program should then randomly quiz the user by displaying the name of a state and asking
the usr to enter that state's capital.
The program should keep a count of the number of correct and incorrect responses.
(As an alternative to the US states, the program can use the names of countries and
their capitals.)"""
0 0
Add a comment Improve this question Transcribed image text
Answer #1

# do comment if any problem arises

# code

import random

# open file

infile = open("state_capitals.txt", "r")

states_capitals = {}

# read all lines

lines = infile.readlines()

# close file

infile.close()

i = 0

# create a dictionary using while loop

while i < len(lines):

    # state

    state = lines[i]

    state = state.rstrip('\n')

    # capital

    capital = lines[i+1]

    capital = capital.rstrip('\n')

    # go to next to next line

    i += 2

    # add to dictionary

    states_capitals[state] = capital

# create a list of states for choosing random state

states = list(states_capitals.keys())

score = 0

# ask user 5 questions

for i in range(5):

    # choose random state

    state = states[random.randint(0, len(states)-1)]

    # read answer of user

    capital = input(f'What is capital of {state}: ')

    capital = capital.lower()

    answer = states_capitals[state].lower()

    # if user answer is correct

    if capital == answer:

        score += 1

# print score

print(f'Your score is: {score}')

Screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
Create a program that solves programming exercise below. Within this folder is the file state_capitals.txt. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program using Python, following the steps below: 1) Collect the Users name, and store...

    Create a program using Python, following the steps below: 1) Collect the Users name, and store that name. 2) Welcome the user, and ask if that user would like to take the Capitals quiz. 3) Create a dictionary of US states as keys and the state capitals that correspond with your list, as the values (Choose 12 of your favorite states). 4) Then have the program quiz the user, randomly asking for the capital of each of the states in...

  • For this lab, write a program that lets the user enter a state and the program...

    For this lab, write a program that lets the user enter a state and the program returns that state's capital, stored in a dictionary Requirements: Start by hard coding in all of the states and their capitals the user should be able to enter the name of a state or ask for all states to be printed (one key/value pair per line) If the state is in the dictionary, print its state capital in the form: "The capital of Vermont...

  • Create a folder named "TrainerApp". In that folder create the following files. Create a PHP file,...

    Create a folder named "TrainerApp". In that folder create the following files. Create a PHP file, "insert-user-form.php", with a form that has the following fields: - First Name (text) - Last Name (text) - Email (text) - Password (text) - Submit button Create another PHP file, "insert-exercise-form.php" with a form that has the following fields: - Exercise Name (text) - Description (text) - Demonstration Image (file) - Submit button Create another PHP file, "login-user-form.php" with a form that has the...

  • Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...

    Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns,...

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • C++, data structure Write a program that plays a game called "guess the state, guess the...

    C++, data structure Write a program that plays a game called "guess the state, guess the capital". How do you play? The program randomly selects a state or a state capital. The program asks the player to guess the state's capital (or the capital's state). The program reads the user's guess; and, tells the user if its guess is right, or if the guess is wrong, tells the user that its guess is wrong and displays the correct answer. Before...

  • Python Problem: To feel good about programming, one should really develop a program that is interesting...

    Python Problem: To feel good about programming, one should really develop a program that is interesting enough to feel good about. Here are some recommended choices for a program to turn in at the end of the semester. The intent here is not to intimidate you about the course demands, but to provide a focal point for your mental energies throughout the term. Only one project is required per student. You do not need to turn in all three. A...

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

  • programming language is C++. Please also provide an explanation of how to create a file with...

    programming language is C++. Please also provide an explanation of how to create a file with the given information and how to use that file in the program Let's consider a file with the following student information: John Smith 99.0 Sarah Johnson 85.0 Jim Robinson 70.0 Mary Anderson 100.0 Michael Jackson 92.0 Each line in the file contains the first name, last name, and test score of a student. Write a program that prompts the user for the file name...

  • a) In Atom, create a new program file named greetByfullName.jsin your pl folder. b) Write a...

    a) In Atom, create a new program file named greetByfullName.jsin your pl folder. b) Write a function named greetByfullName that accepts two strings (firstName and lastName), and returns a cheery greeting. c) Test the function by prompting the user to enter their first name, and storing the name in a variable, and then prompting the user for their last name, and storing it in a second variable. This page says Enter your first name Susan Cancel OK This page says...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT