Monuments of a city

EasyIf-Else Programs · 59 of 64

Write a Python program that accepts a city name (Pune or Mumbai) and displays its monuments.

Advertisement
Input
Pune
Output
Enter city name: Pune
Shaniwar Wada
Lal Mahal
Sinhagad Fort

Approach

  1. Compare the city name and print the matching monuments.

Solution

Python
city = input("Enter city name: ").strip().title()if city == "Pune":    monuments = ["Shaniwar Wada", "Lal Mahal", "Sinhagad Fort"]elif city == "Mumbai":    monuments = ["Gateway of India", "Chhatrapati Shivaji Terminus", "Elephanta Caves"]else:    monuments = [] if monuments:    for m in monuments:        print(m)else:    print("No data for", city)
Outputtyped input: Pune
Enter city name: Pune
Shaniwar Wada
Lal Mahal
Sinhagad Fort

Line by line

Line 1

title() makes “pune” and “PUNE” match “Pune”.

Line 9

An empty list is falsy, so the else branch handles unknown cities.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs