Practical Coding in Python

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Hello random

Coding Exercises: Strings and Math

1. Greetings.

Write a function greetings() that takes a single String name and returns returns a greeting using the given name. Be sure to include unit tests.

Sample output:

greetings("Dr. Kessner") -> "Hello, Dr. Kessner, how are you?"
greetings("Ascii Cat") -> "Hello, Ascii Cat, how are you?"
greetings("Sydneys") -> "Hello, Sydneys, how are you?"

2. Attention.

Write a function attention() that takes a single String as input and returns true if the string starts with “Hey you!”. Be sure to include unit tests.

Sample output:

attention("Hello, my name is Inigo Montoya.") -> false
attention("Excuse me, Dr. Kessner?") -> false
attention("Hey you! Give me your code!" -> true

3. Coin flip.

Write a function that flips a coin randomly, returning a String, either “Heads” or “Tails”. Functions involving randomness are a little tricky to write unit tests for. So you should just have your main() function print the results from 10 or 20 coin flips to try out your function.

4. Die rolling

Write a function that returns the result of rolling a single 6-sided die. In other words, when you call the function, it should randomly return 1, 2, 3, 4, 5, or 6.


Next: