Skip to content

Commit

Permalink
Merge pull request #75 from code4tomorrow/chrehall68-patch-1
Browse files Browse the repository at this point in the history
The other JSON practice problem
  • Loading branch information
Citrus716 authored Mar 8, 2021
2 parents fd77105 + 858faad commit 4212c37
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
8 changes: 8 additions & 0 deletions 3_advanced/chapter20/practice/favorite_foods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"favorite foods": {
"Jerry": "ice cream",
"Ben": "ice cream",
"Steven": "eggroll",
"Spongebob": "Krabby Patty"
}
}
4 changes: 4 additions & 0 deletions 3_advanced/chapter20/practice/json_practice_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# use the file "wildlife.json"
# load the data in the JSON file
# add at least one habitat and corresponding animal(s) to the dictionary
# finally, write the updated dictionary to the json file.
6 changes: 6 additions & 0 deletions 3_advanced/chapter20/practice/wildlife.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"China": "pandas",
"Africa": "cheetas",
"North America": "bison",
"South America": "boa constrictor"
}
14 changes: 14 additions & 0 deletions 3_advanced/chapter20/solutions/favorite_foods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"favorite foods": {
"Jerry": "ice cream",
"Ben": "ice cream",
"Steven": "eggroll",
"Spongebob": "Krabby Patty"
},
"names": [
"Jerry",
"Ben",
"Steven",
"Spongebob"
]
}
5 changes: 4 additions & 1 deletion 3_advanced/chapter20/solutions/json_practice_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
a = open("favorite_foods.json", "r")
x = json.load(a)
names = []
foods = set()
for name, food in x["favorite foods"].items():
print(food)
foods.add(food)
names.append(name)
for food in foods:
print(food)
x["names"] = names # create an item within the dictionary that has the names
a.close()
n = open("favorite_foods.json", "w")
Expand Down
14 changes: 14 additions & 0 deletions 3_advanced/chapter20/solutions/json_practice_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# use the file "wildlife.json"
# load the data in the JSON file
# add at least one habitat and corresponding animal(s) to the dictionary
# finally, write the updated dictionary to the json file.

import json

a = open("wildlife.json", "r")
x = json.load(a)
a.close()
x["Deepest Peru"] = "Paddington"
n = open("wildlife.json", "w")
json.dump(x, n, indent=4)
n.close()
7 changes: 7 additions & 0 deletions 3_advanced/chapter20/solutions/wildlife.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"China": "pandas",
"Africa": "cheetas",
"North America": "bison",
"South America": "boa constrictor",
"Deepest Peru": "Paddington"
}

0 comments on commit 4212c37

Please sign in to comment.