owned this note
owned this note
Published
Linked with GitHub
###### tags: `Lesson Notes` `Python` `Fundamentals` `PythonFlask2021`
# Loops and Conditionals
If you don't like loops and conditionals sorry, but this is a fact of a programmers life. It is just something we have to do. Looping through data and checking to see if something is there or not just happens to be a part of your life now.
## Starter Code:
```
data = [
{
"house": "First House", "floors": [
{
"floor": "1st Floor", "rooms": [
{"room": "Living Room", "contents": ['couch', 'TV', 'painting']},
{"room": "Kitchen", "contents": ['table', 'chairs', 'stove', 'fridge']},
{"room": "Bathroom", "contents": ['sink', 'mirror', 'toilet']}
]
},
{
"floor": "2nd Floor", "rooms": [
{"room": "Master Bedroom", "contents": ['bed', 'dresser', 'clothes']},
{"room": "Kids Room", "contents": ['bed', 'dresser', 'desk', 'clothes']},
{"room": "Bathroom", "contents": ['tub', 'sink', 'toilet', 'mirror']}
]
}
]
},
{
"house": "Second House", "floors": [
{
"floor": "1st Floor", "room": [
{"room": "Den", "contents": ['Bar', 'Bar Stools', 'TV', 'Couch']},
{"room": "Living Room", "contents": ['couch', 'TV', 'rug', 'end tables']}
]
},
{
"floor": "2nd Floor", "rooms": [
{"room": "Bedroom", "contents": ['bed', 'dresser', 'clothes']},
{"room": "Bathroom", "contents": ['sink', 'toilet', 'tub']}
]
}
]
}
]
```
## Loops:
- Ok so we have our starter code that contains data on 2 houses. So lets start our loop demo by printing just the 2 house names.
```
print(data[0]['house'])
print(data[0]['house'])
# Output:
First House
Second House
```
- Yea ok so that works. But we needed 2 print statements and had to know how many houses were in our database...this is where loops come in
```
def houseNames(house):
for h in house:
print("Printing house names: ", h['house'])
houseNames(data)
# Output:
Printing house names: First House
Printing house names: Second House
```
- Ok yes a little more code but now no matter how big our database is we can call this function and get all the house names.
- So lets print all the house names and floor names
```
def allFloors(data):
for d in data:
print("House: ", d['house'])
for f in d['floors']:
print("Floors: ", f['floor'])
allFloors(data)
# Output:
House: First House
Floors: 1st Floor
Floors: 2nd Floor
House: Second House
Floors: 1st Floor
Floors: 2nd Floor
```
- Ok got it. The key is knowing how to access each item that you want to print.
## Conditionals:
- Ok so day we want to check to make sure the Den is listed in the right house and the right floor?
```
def printDen(data):
if data[1]['floors'][0]['rooms'][0]['room'] == 'Den':
print("The Den is at this spot")
else:
print('Sorry you did not find the den')
printDen(data)
# Output:
"The Den is at this spot"
```
- Ok but how do we know that worked? Lets leave the path the same but look instead for the living room
```
def printLivingRoom(data):
if data[1]['floors'][0]['rooms'][0]['room'] == 'Living Room':
print("You have found the living room")
else:
print('Sorry you found a different room')
printLivingRoom(data)
# Output:
'Sorry you found a different room'
```
- Not too bad I guess.
## Combo
- Using the same code lets do a little bit more to it. Use a loop and conditional.
```
def loopsConditional(data, a):
for d in data:
print("Print me the house name please: ", d['house'])
if d['house'] == a:
for f in d['floors']:
print("Floors: ", f['floor'])
else:
print('Sorry this is not the house you are looking for')
loopsConditional(data, "Second House")
# Output:
Print me the house name please: First House
Sorry this is not the house you are looking for
Print me the house name please: Second House
Floors: 1st Floor
Floors: 2nd Floor
```
# Code Links
- [Loops](https://pythontutor.com/visualize.html#code=data%20%3D%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22house%22%3A%20%22First%20House%22,%20%22floors%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%221st%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Living%20Room%22,%20%22contents%22%3A%20%5B'couch',%20'TV',%20'painting'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Kitchen%22,%20%22contents%22%3A%20%5B'table',%20'chairs',%20'stove',%20'fridge'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'sink',%20'mirror',%20'toilet'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%222nd%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Master%20Bedroom%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'clothes'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Kids%20Room%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'desk',%20'clothes'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'tub',%20'sink',%20'toilet',%20'mirror'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D,%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22house%22%3A%20%22Second%20House%22,%20%22floors%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%221st%20Floor%22,%20%22room%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Den%22,%20%22contents%22%3A%20%5B'Bar',%20'Bar%20Stools',%20'TV',%20'Couch'%5D%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Living%20Room%22,%20%22contents%22%3A%20%5B'couch',%20'TV',%20'rug',%20'end%20tables'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%222nd%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bedroom%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'clothes'%5D%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'sink',%20'toilet',%20'tub'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%0A%5D%0A%0Aprint%28data%29%0Aprint%28data%5B0%5D%29%0Aprint%28data%5B0%5D%5B'house'%5D%29%0Aprint%28data%5B1%5D%5B'house'%5D%29%0A%0Adef%20houseNames%28house%29%3A%0A%20%20%20%20for%20h%20in%20house%3A%0A%20%20%20%20%20%20%20%20print%28%22Printing%20house%20names%3A%20%22,%20h%5B'house'%5D%29%0AhouseNames%28data%29%0A%0Adef%20allFloors%28data%29%3A%0A%20%20%20%20for%20d%20in%20data%3A%0A%20%20%20%20%20%20%20%20print%28%22House%3A%20%22,%20d%5B'house'%5D%29%0A%20%20%20%20%20%20%20%20for%20f%20in%20d%5B'floors'%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%28%22Floors%3A%20%22,%20f%5B'floor'%5D%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%0AallFloors%28data%29%0A%0A&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
- [Conditionals](https://pythontutor.com/visualize.html#code=data%20%3D%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22house%22%3A%20%22First%20House%22,%20%22floors%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%221st%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Living%20Room%22,%20%22contents%22%3A%20%5B'couch',%20'TV',%20'painting'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Kitchen%22,%20%22contents%22%3A%20%5B'table',%20'chairs',%20'stove',%20'fridge'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'sink',%20'mirror',%20'toilet'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%222nd%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Master%20Bedroom%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'clothes'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Kids%20Room%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'desk',%20'clothes'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'tub',%20'sink',%20'toilet',%20'mirror'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D,%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22house%22%3A%20%22Second%20House%22,%20%22floors%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%221st%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Den%22,%20%22contents%22%3A%20%5B'Bar',%20'Bar%20Stools',%20'TV',%20'Couch'%5D%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Living%20Room%22,%20%22contents%22%3A%20%5B'couch',%20'TV',%20'rug',%20'end%20tables'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%222nd%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bedroom%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'clothes'%5D%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'sink',%20'toilet',%20'tub'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%0A%5D%0Aprint%28data%5B1%5D%5B'floors'%5D%5B0%5D%5B'rooms'%5D%5B0%5D%5B'room'%5D%29%0A%0Adef%20printDen%28data%29%3A%0A%20%20%20%20if%20data%5B1%5D%5B'floors'%5D%5B0%5D%5B'rooms'%5D%5B0%5D%5B'room'%5D%20%3D%3D%20'Den'%3A%0A%20%20%20%20%20%20%20%20print%28%22The%20Den%20is%20at%20this%20spot%22%29%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print%28'Sorry%20you%20did%20not%20find%20the%20den'%29%0A%0AprintDen%28data%29%0A%0Adef%20printLivingRoom%28data%29%3A%0A%20%20%20%20if%20data%5B1%5D%5B'floors'%5D%5B0%5D%5B'rooms'%5D%5B0%5D%5B'room'%5D%20%3D%3D%20'Living%20Room'%3A%0A%20%20%20%20%20%20%20%20print%28%22You%20have%20found%20the%20living%20room%22%29%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print%28'Sorry%20you%20found%20a%20different%20room'%29%0A%20%20%20%20%20%20%20%20%0AprintLivingRoom%28data%29%0A%0A&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
- [Combo](https://pythontutor.com/visualize.html#code=data%20%3D%20%5B%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22house%22%3A%20%22First%20House%22,%20%22floors%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%221st%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Living%20Room%22,%20%22contents%22%3A%20%5B'couch',%20'TV',%20'painting'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Kitchen%22,%20%22contents%22%3A%20%5B'table',%20'chairs',%20'stove',%20'fridge'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'sink',%20'mirror',%20'toilet'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%222nd%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Master%20Bedroom%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'clothes'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Kids%20Room%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'desk',%20'clothes'%5D%7D,%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'tub',%20'sink',%20'toilet',%20'mirror'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D,%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22house%22%3A%20%22Second%20House%22,%20%22floors%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%221st%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Den%22,%20%22contents%22%3A%20%5B'Bar',%20'Bar%20Stools',%20'TV',%20'Couch'%5D%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Living%20Room%22,%20%22contents%22%3A%20%5B'couch',%20'TV',%20'rug',%20'end%20tables'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22floor%22%3A%20%222nd%20Floor%22,%20%22rooms%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bedroom%22,%20%22contents%22%3A%20%5B'bed',%20'dresser',%20'clothes'%5D%7D,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%22room%22%3A%20%22Bathroom%22,%20%22contents%22%3A%20%5B'sink',%20'toilet',%20'tub'%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%0A%5D%0A%0Adef%20loopsConditional%28data,%20a%29%3A%0A%20%20%20%20for%20d%20in%20data%3A%0A%20%20%20%20%20%20%20%20print%28%22Print%20me%20the%20house%20name%20please%3A%20%22,%20d%5B'house'%5D%29%0A%20%20%20%20%20%20%20%20if%20d%5B'house'%5D%20%3D%3D%20a%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20f%20in%20d%5B'floors'%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print%28%22Floors%3A%20%22,%20f%5B'floor'%5D%29%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%28'Sorry%20this%20is%20not%20the%20house%20you%20are%20looking%20for'%29%0A%0AloopsConditional%28data,%20%22Second%20House%22%29%0A%0A&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)