Skip to content

Adding functions that give age properties of the group #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions excercise8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import json

with open('my_file.json', 'r') as f:
loaded_json_string = f.read()
#print(loaded_json_string)

loaded_data = json.loads(loaded_json_string)
print(f"loaded_data = {loaded_data}")
print(f"type(loaded_data) = {type(loaded_data).__name__}")
print(f"type(loaded_data['foo']) = {type(loaded_data['Jill']).__name__}")

print(type(loaded_json_string))
53 changes: 51 additions & 2 deletions group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
"""An example of how to represent a group of acquaintances in Python."""

# Your code to go here...
# Creates a dictionary of dictionaries...

my_group =
my_group = {"Jill":
{"Job":"Biologist", "Age":26, "Relations":{"Zalika":"Friend", "John":"partner"}},
"Zalika":
{"Job":"Artist", "Age":28, "Relations":{"Jill":"Friend"}},
"John":
{"Job":"Writer", "Age":27, "Relations":{"Zalika":"Partner"}},
"Nash":
{"Job":"Chef", "Age":34, "Relations":{"John":"Cousin","Zalika":"Landlord"}},
"Steve":
{"Job":"Toy", "Age":40, "Relations":{"John":"Friend","Nash":"Owner"}}}

# to access use my_group["Jill"].values() will give values or my_group["Jill"]["Relations"][0]
# ruff from command palette, ctrl shift p and format document will tidy up your code
x=0
i=0
z=0
p=0
w=0
for y in my_group:
i+=1
if my_group[y]["Age"] > x:
x = my_group[y]["Age"]

z+= len(my_group[y]["Relations"])

if len(my_group[y]["Relations"]):
if my_group[y]["Age"] > p:
p= my_group[y]["Age"]

if len(my_group[y]["Relations"]):
for a in my_group[y]["Relations"].values():
if a == "Friend":
if my_group[y]["Age"] > w:
w = my_group[y]["Age"]


print(x) # max age
print(z/i) # mean number of relations
print(p) # max age of people with at least one relation
print(w) # max age of people that have a friend

#Excercise on data formats
import json

json_string = json.dumps(my_group)

with open('my_file.json', 'w') as f:
f.write(json_string)

print(type(my_group))