Walkthrough:
Conditionals Program
Let’s walk through writing a program that helps make decisions when picking out what to pack for a day trip.
Phase 1: Planning with Pseudocode
We’ll start with the following high-level description of our approach, then refine down to pseudocode that is close to what we could implement in Python.
Descriptive level. Write in full prose:
Determine the place you're going (beach, mountain, or park). Get something
to carry your supplies (tote for the beach, backpack for the mountain, picnic
basket for the park). Check the weather to pack appropriately. If it's sunny,
pack sunglasses. If it's sunny and you're going to the beach or the park, pack
sunblock. If it's raining, grab an umbrella. If you're going to the beach, pack
a towel. If you're going to the mountain, pack a flashlight.
Refinement 1. Use line breaks to separate different actions:
Determine the place you're going (beach, mountain, or park).
Get something to carry your supplies
(tote for the beach, backpack for the mountain, picnic basket for the park).
Check the weather and
pack appropriately.
If it's sunny, pack sunglasses.
If it's sunny and you're going to the beach or the park, pack sunblock.
If it's raining, grab an umbrella.
If you're going to the beach, pack a towel.
If you're going to the mountain, pack a flashlight.
Refinement 2. Reorder and reword to make actions sequential:
Determine the place you're going (beach, mountain, or park).
Get something to carry your supplies
(tote for the beach, backpack for the mountain, picnic basket for the park).
Check the weather
pack appropriately.
If it's sunny, pack sunglasses.
If it's sunny and you're going to the beach or the park, pack sunblock.
If it's raining, grab an umbrella.
If you're going to the beach, pack a towel.
If you're going to the mountain, pack a flashlight.
Refinement 3. Make actions simpler and more concise by removing irrelevant phrases:
Determine place (beach, mountain, or park).
Get supply-carrier
(tote for beach, backpack for mountain, picnic basket for park).
Check weather and
pack appropriately.
If sunny, sunglasses.
If sunny and (beach or park), sunblock.
If raining, umbrella.
If beach, towel.
If mountain, flashlight.
Refinement 4. Separate conjunctive phrases to make each line contain exactly one action/instruction:
Determine place (beach, mountain, or park).
Get supply-carrier
(tote for beach, backpack for mountain, picnic basket for park).
Check weather
pack appropriately.
If sunny, sunglasses.
If sunny and (beach or park), sunblock.
If raining, umbrella.
If beach, towel.
If mountain, flashlight.
Refinement 5. Identify conditional and use keyword if; add colons (:) to identify actions (e.g., verbs) and any required objects:
Determine place (beach, mountain, or park)
Get supply-carrier for place
IF beach
get: tote
ELSE IF mountain
get: backpack
ELSE IF park
get: picnic basket
Check weather
Pack for weather, place
IF sunny
pack: sunglasses
IF sunny and (beach or park)
pack: sunblock
IF raining
pack: umbrella
IF beach
pack: towel
IF mountain
pack: flashlight
Phase 2: Implementing in Python
Turn each line of pseudocode into a comment
Translate each line into Python
def getSupplyCarrier( place ):
""" Given a place ("beach", "mountain" or "park"),
get the right supply carrier"""
# IF beach
if place == "beach":
# get: tote
return "tote"
# ELSE IF mountain
elif place == "mountain":
# get: backpack
return "backpack"
# ELSE IF park
elif place == "park":
# get: picnic basket
return "picnic basket"
# placeholder return
return "TBD carrier"
def packFor( weather, place ):
""" Given the weather ("sunny" or "raining")
and a place ("beach", "mountain" or "park"),
pack the right supplies!"""
supplyList = "" # start with no supplies
# IF sunny
if weather == "sunny":
# pack: sunglasses
supplyList = supplyList + "sunglasses, "
# IF sunny and (beach or park)
if weather == "sunny" and ((place == "beach") or (place == "park")):
# pack: sunblock
supplyList = supplyList + "sunblock, "
# IF raining
if weather == "raining":
# pack: umbrella
supplyList = supplyList + "umbrella, "
# IF beach
if place == "beach":
# pack: towel
supplyList = supplyList + "towel, "
# IF mountain
if place == "mountain":
# pack: flashlight
supplyList = supplyList + "flashlight, "
# take off extra ", "
supplyList = supplyList[0:len(supplyList)-2]
return supplyList
# Determine: place (beach, mountain, or park)
place = input( "Where are you going today? [beach/mountain/park]: ")
# Get: supply carrier
carrier = getSupplyCarrier( place )
# Check: weather
weather = input( "What is the weather like? [sunny/raining]: ")
# Pack for: weather, place
packItems = packFor( weather, place )
print( "Get a " + carrier + " and put your " + packItems + " inside. Enjoy your trip!" )