Challenge hints & worked solutions
CHALLENGE 031 · BUILD IT
Name Initials
Create a name-badge generator. Ask for a first name and a surname, then show their initials in the format A. L. For this challenge, each name is one word, although spaces may appear around it.
Reveal a hint (from the printed book)
Use .strip() and check that both names are non-empty before looking up name[0].
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 31 - Name Initials
first = input("First name: ").strip()
last = input("Surname: ").strip()
if not first or not last:
print("Please enter both names.")
else:
print(f"Initials: {first[0].upper()}. {last[0].upper()}.")
Save this Python file ↓CHALLENGE 032 · IMPROVE IT
Shout and Whisper
The starter displays a sentence in upper case and lower case. Extend it to display title case and to count letters, but do not remove either original output. Punctuation, numbers and spaces are not letters.
Reveal a hint (from the printed book)
Count characters one at a time. isalpha() distinguishes letters from spaces, digits and punctuation.
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 32 - Shout and Whisper
message = input("Enter a sentence: ")
print("Upper:", message.upper())
print("Lower:", message.lower())
print("Title:", message.title())
letters = 0
for character in message:
if character.isalpha():
letters += 1
print(f"Letters: {letters}")
Save this Python file ↓CHALLENGE 033 · DEBUG IT
Tidy the Name
The faulty starter changes letter case but leaves unwanted spaces at the start and between words. Repair it so the cleaned name has single spaces between words and title case. Treat all-space input as missing.
Reveal a hint (from the printed book)
Try name.split() to make a list of non-empty words, then join them back with one space.
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 33 - Tidy the Name
name = input("Enter a name: ")
words = name.split()
if not words:
print("Please enter a name.")
else:
cleaned = " ".join(words).title()
print(f"Tidy name: {cleaned}")
Save this Python file ↓CHALLENGE 034 · BUILD IT
Create a Username
Invent a short username using this exact rule: the first three letters of the first name followed by the first three letters of the surname, all lower case. Shorter names contribute all available letters.
Reveal a hint (from the printed book)
Slice each cleaned name with [:3], convert to lowercase and concatenate.
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 34 - Create a Username
first = input("First name: ").strip().lower()
last = input("Surname: ").strip().lower()
if not first or not last:
print("Please enter both names.")
else:
username = first[:3] + last[:3]
print(f"Username: {username}")
Save this Python file ↓CHALLENGE 035 · SOLVE IT
Text Message Counter
A fictional messaging service limits each message to 100 characters, including spaces and punctuation. Count the typed characters and explain whether the message is within, exactly at, or beyond the limit.
Reveal a hint (from the printed book)
Compare len(message) against 100, and test below, at and above the limit.
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 35 - Text Message Counter
message = input("Message: ")
count = len(message)
print(f"Characters: {count}")
if count < 100:
print(f"Within limit. Remaining: {100 - count}")
elif count == 100:
print("At limit. Remaining: 0")
else:
print(f"Over limit by: {count - 100}")
Save this Python file ↓CHALLENGE 036 · BUILD IT
Vowel Detective
Write a program to count the letters a, e, i, o and u in a sentence, regardless of case. Treat every other character, including y, as a non-vowel for this challenge.
Reveal a hint (from the printed book)
Set a counter to zero, convert the sentence to lowercase and use character in "aeiou".
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 36 - Vowel Detective
message = input("Sentence: ")
total = 0
for character in message.lower():
if character in "aeiou":
total += 1
print(f"Vowels: {total}")
Save this Python file ↓CHALLENGE 037 · IMPROVE IT
Reverse the Message
The starter reverses the entire message, putting the words in reverse order. Improve it so the characters in each word are reversed but the word order stays the same. Preserve ordinary spaces and repeated spaces.
Reveal a hint (from the printed book)
Split on an explicit space, reverse each word with [::-1], then join using a single-space separator.
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 37 - Reverse the Message
message = input("Message: ")
words = message.split(" ") # Keeps empty entries between repeated spaces
reversed_words = []
for word in words:
reversed_words.append(word[::-1])
print("Reversed words:", " ".join(reversed_words))
Save this Python file ↓CHALLENGE 038 · SOLVE IT
Palindrome Checker
A palindrome reads the same forwards and backwards. Check a phrase without changing the original input: ignore spaces, punctuation and capital letters. Keep only letters and digits before comparing.
Reveal a hint (from the printed book)
Build a new cleaned string using isalnum(), then compare it with its reverse. Handle empty cleaned text separately.
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 38 - Palindrome Checker
phrase = input("Phrase: ")
cleaned = ""
for character in phrase.lower():
if character.isalnum():
cleaned += character
if not cleaned:
print("No letters or digits to check.")
elif cleaned == cleaned[::-1]:
print("Palindrome: yes")
else:
print("Palindrome: no")
Save this Python file ↓CHALLENGE 039 · BUILD IT
Find the Hidden Word
Ask for a sentence and a keyword. Report whether the keyword appears anywhere in that sentence, ignoring upper- and lower-case differences. In this challenge, a match inside a longer word also counts.
Reveal a hint (from the printed book)
Reject an empty keyword before searching. Normalise both strings with casefold().
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 39 - Find the Hidden Word
sentence = input("Sentence: ")
keyword = input("Keyword: ").strip()
if not keyword:
print("Please enter a keyword.")
elif keyword.casefold() in sentence.casefold():
print("Found: yes")
else:
print("Found: no")
Save this Python file ↓CHALLENGE 040 · DEBUG IT
The Broken Message Splitter
A program attempts to display the first two words in a message, but it crashes if there are fewer than two words. Diagnose the IndexError and repair it to handle blank, single-word and multi-word messages.
Reveal a hint (from the printed book)
Use len(words) to decide which positions exist before indexing any elements.
Reveal a worked Python solution
One possible implementation. Check the book's tests and try an extension after comparing.
# Challenge 40 - The Broken Message Splitter
message = input("Message: ")
words = message.split()
if len(words) == 0:
print("No words found.")
elif len(words) == 1:
print(f"First word: {words[0]}")
print("No second word.")
else:
print(f"First word: {words[0]}")
print(f"Second word: {words[1]}")
Save this Python file ↓