Skip to content

Commit a54ae35

Browse files
committed
Address review feedback on sprint 5 exercises
1 parent 1e88ac5 commit a54ae35

4 files changed

Lines changed: 43 additions & 16 deletions

File tree

‎sprint-5-exercises/enumm.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,20 @@ class Laptop:
7474

7575

7676
# Convert the user's input into an OperatingSystem enum value.
77-
# If the value isn't one of our enum choices, exit with an error.
78-
try:
79-
preferred_operating_system = OperatingSystem(
80-
input("What is your preferred operating system? ")
81-
)
82-
except ValueError:
83-
print("Invalid operating system.", file=sys.stderr)
84-
sys.exit(1)
77+
# If the input is invalid, ask the user to try again.
78+
while True:
79+
try:
80+
preferred_operating_system = OperatingSystem(
81+
input(
82+
"What is your preferred operating system "
83+
"(macOS, Arch Linux, or Ubuntu)? "
84+
)
85+
)
86+
break
87+
except ValueError:
88+
print(
89+
"Invalid operating system. " "Please choose macOS, Arch Linux, or Ubuntu."
90+
)
8591

8692

8793
# Create a Person using the validated input.

‎sprint-5-exercises/method.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ def is_adult(self):
1111
today = date.today()
1212
age = today.year - self.date_of_birth.year
1313

14+
# check if ones birthday has happened yet in this year;
15+
if (today.month, today.day) < (
16+
self.date_of_birth.month,
17+
self.date_of_birth.day,
18+
):
19+
age -= 1
20+
1421
return age >= 18
1522

1623

‎sprint-5-exercises/type-checking-mypy.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
def open_account(balances, name, amount):
1+
def open_account(balances: dict[str, int], name: str, amount: int) -> None:
22
balances[name] = amount
33

44

5-
def sum_balances(accounts):
5+
def sum_balances(accounts: dict[str, int]) -> int:
66
total = 0
77
for name, pence in accounts.items():
88
print(f"{name} had balance {pence}")
99
total += pence
1010
return total
1111

1212

13-
def format_pence_as_string(total_pence):
13+
def format_pence_as_string(total_pence: int) -> str:
1414
if total_pence < 100:
1515
return f"{total_pence}p"
1616
pounds = int(total_pence / 100)
@@ -24,8 +24,8 @@ def format_pence_as_string(total_pence):
2424
"Georg": 831,
2525
}
2626

27-
open_account(balances, "Tobi", 9.13)
28-
open_account(balances, "Olya", "£7.13")
27+
open_account(balances, "Tobi", 913)
28+
open_account(balances, "Olya", 713)
2929

3030
total_pence = sum_balances(balances)
3131
total_string = format_pence_as_string(total_pence)
@@ -39,3 +39,10 @@ def format_pence_as_string(total_pence):
3939
# type-checking-mypy.py:31: error: Name "format_pence_as_str" is not defined [name-defined]
4040

4141
# to fix the bug we need to add the positional argument in open account function and call the correct name of the second function. "format_pence_as_string"
42+
43+
# After adding type annotations to the functions and testing with mypy I got the following errors:
44+
# sprint-5-exercises/type-checking-mypy.py:27: error: Argument 3 to "open_account" has incompatible type "float"; expected "int" [arg-type].
45+
# ========> To fix it, we should change the argument type from float to integer.
46+
47+
# sprint-5-exercises/type-checking-mypy.py:28: error: Argument 3 to "open_account" has incompatible type "str"; expected "int" [arg-type]
48+
# ==========> to fix this issue we should change the argument type from string to integer.

‎sprint-5-exercises/type-guided-refactoring.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class Laptop:
2121
def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
2222
possible_laptops = []
2323
for laptop in laptops:
24-
if laptop.operating_system == person.preferred_operating_systems:
24+
if laptop.operating_system in person.preferred_operating_systems:
2525
possible_laptops.append(laptop)
2626
return possible_laptops
2727

2828

2929
people = [
30-
Person(name="Imran", age=22, preferred_operating_systems="Ubuntu"),
31-
Person(name="Eliza", age=34, preferred_operating_systems="Arch Linux"),
30+
Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu"]),
31+
Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]),
3232
]
3333

3434
laptops = [
@@ -65,3 +65,10 @@ def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]
6565
for person in people:
6666
possible_laptops = find_possible_laptops(laptops, person)
6767
print(f"Possible laptops for {person.name}: {possible_laptops}")
68+
69+
# After testing with mypy I found 2 errors:
70+
# sprint-5-exercises/type-guided-refactoring.py:30: error: Argument "preferred_operating_systems" to "Person" has incompatible type "str"; expected "list[str]" [arg-type].
71+
# =======> to fix the issue we should have list of string instead of str itself.
72+
73+
# sprint-5-exercises/type-guided-refactoring.py:31: error: Argument "preferred_operating_systems" to "Person" has incompatible type "str"; expected "list[str]" [arg-type].
74+
# ========> to fix the issue we should have list of string instead of str itself.

0 commit comments

Comments
 (0)