Home

Data Abstraction Homework

grade = 10

print("I am in grade " + str(grade))
I am in grade 10

Popcorn Hack 2


testNum = 15

print("Int is ", testNum)
print("String is " + str(testNum))
print("Bool is " + str(bool(testNum)))
print("Float is ", float(testNum))

Int is  15
String is 15
Bool is True
Float is  15.0

Popcorn Hack 3


a = 10
b = 20.5
c = 'Hello'

print("Initial values:")
print("a =", a)
print("b =", b)
print("c =", c)

a, b, c = b, c, a

print("\nUpdated values:")
print("a =", a, " (type:", type(a).__name__, ")")
print("b =", b, " (type:", type(b).__name__, ")")
print("c =", c, " (type:", type(c).__name__, ")")

Initial values:
a = 10
b = 20.5
c = Hello

Updated values:
a = 20.5  (type: float )
b = Hello  (type: str )
c = 10  (type: int )

Popcorn Hack 4


my_interests = ['coding', 'biking', 'cooking', 'video games', 'friends and family']

for interest in my_interests:
    print(interest)

coding
biking
cooking
video games
friends and family

Popcorn Hack 5


import json

interests = ["Family and Friends", "Biking", "Coding", "Cooking"]

json_obj = json.dumps(interests)

print(json_obj)
["Family and Friends", "Biking", "Coding", "Cooking"]

Homework


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

def print_people(people):
    for person in people:
        print(f'{person.name}: Age {person.age}')

def find_oldest_person(people_dict):
    oldest_age = float('-inf')
    oldest_person = None

    for name, age in people_dict.items():
        if age > oldest_age:
            oldest_age = age
            oldest_person = name

    if oldest_person:
        print(f'The oldest person is: {oldest_person}, Age {oldest_age}')

people_list = [
    Person('Imaad M', 15),
    Person('Anika B', 15),
    Person('Tanay S', 16),
    Person('Ananya A', 16),
    Person('Anika P', 17)
]

people_dict = {
    'Imaad M': 15,
    'Anika B': 15,
    'Tanay S': 16,
    'Ananya A': 16,
    'Anika P': 17
}

print('Names and ages from the list of people:')
print_people(people_list)

print('\nName of the oldest person from the dictionary:')
find_oldest_person(people_dict)

Names and ages from the list of people:



---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb Cell 12 line 3
     <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=23'>24</a> people_dict = {
     <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=24'>25</a>     'Imaad M': 15,
     <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=25'>26</a>     'Anika B': 15,
   (...)
     <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=28'>29</a>     'Anika P': 17
     <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=29'>30</a> }
     <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=31'>32</a> print('Names and ages from the list of people:')
---> <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=32'>33</a> print_people(people_dict)
     <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=34'>35</a> print('\nName of the oldest person from the dictionary:')
     <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=35'>36</a> find_oldest_person(people_dict)


/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb Cell 12 line 8
      <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=5'>6</a> def print_people(people):
      <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=6'>7</a>     for person in people:
----> <a href='vscode-notebook-cell:/Users/imaadmuzaffer/vscode/student2/_notebooks/2023-10-05-dataAbstraction.ipynb#X14sZmlsZQ%3D%3D?line=7'>8</a>         print(f'{person.name}: Age {person.age}')


AttributeError: 'str' object has no attribute 'name'
© 2024    •  Powered by Soopr   •  Theme  Moonwalk