PYTHON. Write a program that requests the name of a state as input and displays
ID: 3818444 • Letter: P
Question
PYTHON. Write a program that requests the name of a state as input and displays the abbreviation, nickname, and capital of the state.In Exercises 21 and 22, use the file Oscars.txt that contains the names and genres of each film that won an Oscar for best picture of 1928 through 2013. The films are listed in the order they received the award. The first three lines of the file are
Wings,silent
The Broadway Melody,musical
All Quiet on the Western Front,war
Contents of oscars.txt
Wings,silent
The Broadway Melody,musical
All Quiet on the Western Front,war
Cimarron,western
Grand Hotel,drama
Cavalcade,drama
It Happened One Night,romance
Mutiny on the Bounty,adventure
The Great Ziegfeld,musical
The Life of Emile Zola,biopic
You Can't Take It With You,comedy
Gone with the Wind,epic
Rebecca,thriller
How Green Was My Valley,drama
Mrs. Miniver,war
Casablanca,drama
Going My Way,musical
The Lost Weekend,drama
The Best Years of Our Lives,war
Gentleman's Agreement,drama
Hamlet,drama
All the King's Men,drama
All About Eve,drama
An American in Paris,musical
The Greatest Show on Earth,epic
From Here to Eternity,war
On the Waterfront,crime
Marty,romance
Around the World in 80 Days,adventure
The Bridge on the River Kwai,war
Gigi,musical
Ben-Hur,epic
The Apartment,comedy
West Side Story,musical
Lawrence of Arabia,biopic
Tom Jones,comedy
My Fair Lady,musical
The Sound of Music,musical
A Man for All Seasons,biopic
In the Heat of the Night,crime
Oliver,musical
Midnight Cowboy,drama
Patton,biopic
The French Connection,crime
The Godfather,crime
The Sting,comedy
The Godfather Part II,crime
One Flew Over the Cuckoo’s Nest,drama
Rocky,sports
Annie Hall,comedy
The Deer Hunter,war
Kramer vs. Kramer,drama
Ordinary People,drama
Chariots of Fire,drama
Gandhi,biopic
Terms of Endearment,comedy
Amadeus,biopic
Out of Africa,biopic
Platoon,war
The Last Emperor,biopic
Rain Man,drama
Driving Miss Daisy,comedy
Dances with Wolves,western
The Silence of the Lambs,thriller
Unforgiven,western
Schindler's List,biopic
Forrest Gump,comedy
Braveheart,war
The English Patient,war
Titanic,romance
Shakespeare in Love,romance
American Beauty,drama
Gladiator,epic
A Beautiful Mind,biopic
Chicago,musical
The Lord of the Rings:The Return of the King,fantasy
Million Dollar Baby,sports
Crash,drama
The Departed,crime
No Country for Old Men,crime
Slumdog Millionaire,romance
The Hurt Locker,war
The King's Speech,biopic
The Artist,silent
Argo,drama
12 Years a Slave,biopic
Explanation / Answer
# pastebin link for code: https://pastebin.com/QAxEjkA7
'''
This assume that there is a file named state.txt
which contain data in format state,abberviation,nickname, cpaital
First few lines
Ohio, OH, Buckeye State, Columbus
Alabama, AL, The Yellowhammer State, Montgomery
Alaska, AK, The Last Frontier, Juneau
Arizona, AZ, The Grand Canyon State, Phoenix
Sample output
Enter the name of a state: Ohio
Abbreviation: OH
Nickname: Buckeye State
Capital: Columbus
'''
state_info = {}
with open("states.txt", "r") as fh:
for line in fh:
info = line.split(",")
state_info[info[0].strip().lower()] = info[1:]
state = input("Enter the name of a state: ")
state = state.lower()
print("Abbreviation: " + str(state_info[state][0]))
print("Nickname: " + str(state_info[state][1]))
print("Capital: " + str(state_info[state][2]))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.