Python Use the file StatesANC.txt that contains the name, abbreviation, nickname
ID: 3910103 • Letter: P
Question
Python
Use the file StatesANC.txt that contains the name, abbreviation, nickname, and capital of each state in the United States. The states are listed in alphabetical order. The first three lines are:
Alabama, AL, Cotton State, Montgomery
Alaska, AK, The Last Frontier, Juneau
Arizona, AZ, Grand Canyon State, Phoenix
Write a program that requests the name of a state as input and displays the abbreviation, nickname, and capitol of the state.
Possiable Outcome
Enter the name of a state: Ohio
Abbreviation: OH
Nickname: Buckeye State
Capital: Columbus
Explanation / Answer
def main(): with open('StatesANC.txt', 'r') as f: states = [] for line in f: words = line.strip().split(',') states.append((words[0].strip(), words[1].strip(), words[2].strip(), words[3].strip())) name = input('Enter the name of a state: ') state = None for s in states: if s[0] == name: state = s break if state is not None: print('Abbreviation: ' + state[1]) print('Nickname: ' + state[2]) print('Capital: ' + state[3]) main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.