Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***Python 3*** Sorry if poor grammar or mistakes; A word focused answer would be appreciated, rather than to give a direct solution! Thanks ------------------------------------------------------------------------------------------------------ (Chapter

***Python 3***

Sorry if poor grammar or mistakes; A word focused answer would be appreciated, rather than to give a direct solution! Thanks

------------------------------------------------------------------------------------------------------

(Chapter 7, Gaddis 4th ed.)Write a program that asks user for a city name, compares that value with the contents of two text files, and reports each match (if any). Define a function to handle importing file data into a list.

------------------------------------------------------------------------------------------------------

Step 1: Define function

a) Function should be named file2list.

b) open the file for reading and read each city name, and store in the list.

c) Handle IOError exception by printing an error message and returning an empty list

d) Use try-except-else construction and remember to close the file. (Output should be a list populated with the city names for that state)

------------------------------------------------------------------------------------------------------

#Code to test function:#

mylist-file2list('stateSample1_municipalitiesList.txt')

print('Your State1 list contains' , len(mylist) , 'elements.')

mylist=file2list('sample2_municList.txt')

print('Your State2 list contains' , len(mylist) , 'elements.')

#Test output if State2 file spelled wrong#

Your State1 list contains 130 elements.

***FILE ERROR: sample2_municList.txt cannot be found.

Your State2 list contains 0 elements.

------------------------------------------------------------------------------------------------------

Step 2: Use main() for user interaction

a) Populate two lists, one each with the contents of vt_municipalities.txt and nh_municipalities.txt. To do this, call your file2list function.

b) Use len function to be sure your lists are not empty.

c) If either list is empty, do nothing. Otherwise, prompt the user for a city name and report the results as shown. Continuing prompting for additional city names until user enters 'q' for quit.

#Sample Run:#

City name (type 'q' to quit): Springfield

Springfield is in State1 and State2.

City name (type 'q' to quit): Gdask

Neither State1 nor State2 has a city by that name.

City name (type 'q' to quit): q

>>>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Example. Evaluate 5n+7 lim 7-00 3n-5

Answered: 1 week ago