You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Already has a list of Laptops that a library has to lend out.
5
+
# Accepts user input to create a new Person - it should use the input function to read a person’s name, age, and preferred operating system.
6
+
# Tells the user how many laptops the library has that have that operating system.
7
+
# If there is an operating system that has more laptops available, tells the user that if they’re willing to accept that operating system they’re more likely to get a laptop.
8
+
# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible, and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values.
#Read the error, and make sure you understand what it’s telling you.
17
+
18
+
#solution:
19
+
# The error message indicates that the Person class does not have an attribute named "address".
20
+
# This means that when the code tries to access eliza.address and imran.address, it fails because the address attribute was never defined in the Person class.
21
+
# class-and-objects.py:9: error: "Person" has no attribute "address" [attr-defined]
22
+
# class-and-objects.py:13: error: "Person" has no attribute "address" [attr-defined]
23
+
# Found 2 errors in 1 file (checked 1 source file)
24
+
25
+
#exercise 2:
26
+
# Add the is_adult code to the file you saved earlier.
27
+
# Run it through mypy - notice that no errors are reported - mypy understands that Person has a property named age so is happy with the function.
28
+
# Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error.
# When I run mypy on this code, it shows an error related to the get_favorite_color function.
45
+
# class-and-objects.py:42: error: Returning Any from function declared to return "str" [no-any-return]
46
+
# class-and-objects.py:42: error: "Person" has no attribute "favorite_color" [attr-defined]
47
+
# Found 2 errors in 1 file (checked 1 source file)
48
+
# The error message indicates that the Person class does not have an attribute named "favorite_color".
49
+
# This means that when the code tries to access person.favorite_color, it fails because the favorite_color attribute was never defined in the Person class.
# Predict what double("22") will do. Then run the code and check. Did it do what you expected? Why did it return the value it did?
8
+
9
+
#solution:
10
+
# My prediction is that it will return "44" but
11
+
# when I run the code I got "2222" which is different from my prediction
12
+
# because the function takes the string "22" concatenates it with itself, resulting in "2222". When you multiply a string by an integer in Python, it repeats the string that many times.
13
+
14
+
15
+
16
+
#exercise 2:
17
+
#Read the code and write down what the bug is. How would you fix it?
18
+
defdouble(number):
19
+
returnnumber*3
20
+
21
+
print(double(10))
22
+
23
+
#solution:
24
+
# The bug is that the function is supposed to double the input value, but it is actually tripling it by multiplying by 3.
25
+
# To fix it, I would change the multiplication factor from 3 to 2, like this:
26
+
27
+
# def double(number):
28
+
# return number * 2 or
29
+
30
+
# I would rename the function to reflect its actual behavior:
#Fix the below code so that it works. You must not change the print on line 20 - we do want to print the children’s ages. (Feel free to invent the ages of Imran’s children.)
#Play computer with this code. Predict what you expect each line will do. Then run the code and check your predictions. (If any lines cause errors, you may need to comment them out to check later lines).
3
+
#Solution:
4
+
# The first four print statements will work as expected, showing the child's name before and after the last name change.
5
+
# The last four print statements will cause errors because the Parent class does not have the get_full_name method or the change_last_name method.
6
+
# To fix the errors, we can comment out the lines that call these methods on the Parent instance.or we can implement these methods in the Parent class if needed.
# Think of the advantages of using methods instead of free functions. Write them down in your notebook.
3
+
4
+
#solution:
5
+
# 1. Encapsulation: Methods allow for better encapsulation of data and behavior within a class, making it easier to manage and maintain code.
6
+
# 2. Code Organization: Methods help organize code by grouping related functionality together within a class, improving readability and structure.
7
+
# 3. Inheritance and Polymorphism: Methods can be overridden in subclasses, allowing for polymorphic behavior and code reuse through inheritance.
8
+
# 4. Access to Instance Data: Methods have access to the instance's data (attributes), enabling them to operate on the object's state directly.
9
+
# 5. Namespace Management: Methods help avoid naming conflicts by being scoped within the class, reducing the likelihood of global namespace pollution.
10
+
# 6. Improved Collaboration: In team environments, methods within classes can provide clear interfaces for collaboration, making it easier for multiple developers to work on the same codebase.
11
+
# 7. Object-Oriented Design: Methods are a fundamental part of object-oriented programming, promoting principles like encapsulation, abstraction, and modularity.
12
+
13
+
#exercise 2:
14
+
#Change the Person class to take a date of birth (using the standard library’s datetime.date class) and store it in a field instead of age.
15
+
#Update the is_adult method to act the same as before.
# This code contains bugs related to types. They are bugs mypy can catch.
2
+
3
+
# Read this code to understand what it’s trying to do. Add type annotations to the method parameters and return types of this code. Run the code through mypy, and fix all of the bugs that show up. When you’re confident all of the type annotations are correct, and the bugs are fixed, run the code and check it works.
4
+
5
+
# def open_account(balances, name, amount):
6
+
# balances[name] = amount
7
+
8
+
# def sum_balances(accounts):
9
+
# total = 0
10
+
# for name, pence in accounts.items():
11
+
# print(f"{name} had balance {pence}")
12
+
# total += pence
13
+
# return total
14
+
15
+
# def format_pence_as_string(total_pence):
16
+
# if total_pence < 100:
17
+
# return f"{total_pence}p"
18
+
# pounds = int(total_pence / 100)
19
+
# pence = total_pence % 100
20
+
# return f"£{pounds}.{pence:02d}"
21
+
22
+
# balances = {
23
+
# "Sima": 700,
24
+
# "Linn": 545,
25
+
# "Georg": 831,
26
+
# }
27
+
28
+
# open_account("Tobi", 9.13)
29
+
# open_account("Olya", "£7.13")
30
+
31
+
# total_pence = sum_balances(balances)
32
+
# total_string = format_pence_as_str(total_pence)
33
+
34
+
# print(f"The bank accounts total {total_string}")
35
+
36
+
#when I run mypy on this code, it shows several type errors related to the parameters passed to the open_account function.
37
+
# type-checking.py:5: error: Function is missing a type annotation [no-untyped-def]
38
+
# type-checking.py:8: error: Function is missing a type annotation [no-untyped-def]
39
+
# type-checking.py:15: error: Function is missing a type annotation [no-untyped-def]
40
+
# type-checking.py:28: error: Call to untyped function "open_account" in typed context [no-untyped-call]
41
+
# type-checking.py:28: error: Missing positional argument "amount" in call to "open_account" [call-arg]
42
+
# type-checking.py:29: error: Call to untyped function "open_account" in typed context [no-untyped-call]
43
+
# type-checking.py:29: error: Missing positional argument "amount" in call to "open_account" [call-arg]
44
+
# type-checking.py:31: error: Call to untyped function "sum_balances" in typed context [no-untyped-call]
45
+
# type-checking.py:32: error: Name "format_pence_as_str" is not defined [name-defined]
46
+
# Found 9 errors in 1 file (checked 1 source file)
47
+
#To fix these errors, In the open_account function I need to ensure that the balances parameter is a dictionary mapping strings to integers, the name parameter is a string, and the amount parameter is an integer .
48
+
#Additionally, I need to make sure that the values passed to the open_account function are of the correct types.
49
+
#Here is the corrected code with type annotations and fixed function calls:
# Now, when I run mypy on the corrected code, I get no type errors, indicating that all type annotations are correct and the bugs have been fixed.with the result:"# Success: no issues found in 1 source file"
0 commit comments