We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← From Everyday Python to Production Code step 1 of 223
Easy
Primitives
Fix Your First mypy Errors
A teammate wrote two small helpers, and every visible test passes. mypy still rejects the file, and one of its errors comes from a real bug that no visible test catches.
initials(full_name)returns the first letter of each word, in capitals."Ada Lovelace"gives"AL".greeting(name)returns"Hello, Ada!"for the name"ada". WhennameisNone, it should return"Hello, guest!".
What mypy reports
Submit the starter as it is, and mypy reports three errors:
L9:1 [no-untyped-def]
Function is missing a type annotation
L16:24 [union-attr]
Item "None" of "str | None" has no attribute "title"
L22:21 [no-untyped-call]
Call to untyped function "initials" in typed context
What to fix
- Annotate
initials. It takes astrand returns astr. That fixes the first error, and also the third, which comes from calling a function that has no annotations. - In
greeting, return"Hello, guest!"whennameisNone, before the code calls.title(). The hidden tests passNone, so the starter fails them as well as failing mypy.
Keep solve as it is.
What the tests check
initialsgives one capital letter per word and ignores extra spaces. An empty string gives"".greetingputsname.title()between"Hello, "and"!", or returns"Hello, guest!"whennameisNone.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.