Python Workbook 1
Python Workbook 1
python-workbook-1
Created quarta 26 agosto 2020
Function Exercises
Functions allow a programmer to break a problem into pieces that can be reused. They can also help a
programmer focus on one part a larger problem at a time. As a result, writing functions is often an important
part of developing larger pieces of software. The exercises in this list will help you practice these skills:
Your task is to write a program that displays the complete lyrics for The Twelve Days of Christmas. Write a
function that takes the verse number as its only parameter and displays the specified verse of the song. Then
call that function 12 times with integers that increase from 1 to 12.
Each item that is sent to the recipient in the song should only appear once in your program, with the possible
file:///tmp/zim-kv6t8wsq/print-to-browser.html 1/3
8/26/2020 python-workbook-1
exception of the partridge. It may appear twice if that helps you handle the difference between “A partridge
in a pear tree” in the first verse and “And a partridge in a pear tree” in the subsequent verses.
4-Capitalize It
(48 Lines)
Many people do not use capital letters correctly, especially when typing on small devices like smart phones.
In this exercise, you will write a function that capitalizes the appropriate characters in a string. A lowercase
“i” should be replaced with an uppercase “I” if it is both preceded and followed by a space. The first
character in the string should also be capitalized, as well as the first non-space character after a “.”, “!” or
“?”. For example, if the function is provided with the string “what time do i have to be there? what’s the
address?” then it should return the string “What time do I have to be there? What’s the address?”. Include a
main program that reads a string from the user, capitalizes it using your function, and displays the result.
7-Random Password
(33 Lines)
Write a function that generates a random password. The password should have a random length of between 7
and 10 characters. Each character should be randomly selected from positions 33 to 126 in the ASCII table.
Your function will not take any parameters. It will return the randomly generated password as its only result.
Display the randomly generated password in your file’s main program. Your main program should only run
when your solution has not been imported into another file.
Hint: You will probably find the chr function helpful when completing this exercise. Detailed
information about this function is available online.
8-Check a Password
(40 Lines)
In this exercise you will write a function that determines whether or not a password is good. We will define a
good password to be a one that is at least 8 characters long and contains at least one uppercase letter, at least
one lowercase letter, and at least one number. Your function should return true if the password passed to it as
file:///tmp/zim-kv6t8wsq/print-to-browser.html 2/3
8/26/2020 python-workbook-1
its only parameter is good. Otherwise it should return false. Include a main program that reads a password
from the user and reports whether or not it is good. Ensure that your main program only runs when your
solution has not been imported into another file.
11-Reduce Measures
(83 Lines)
Many recipe books still use cups, tablespoons and teaspoons to describe the volumes of ingredients used
when cooking or baking. While such recipes are easy enough to follow if you have the appropriate
measuring cups and spoons, they can be difficult to double, triple or quadruple when cooking Christmas
dinner for the entire extended family. For example, a recipe that calls for 4 tablespoons of an ingredient
requires 16 tablespoons when quadrupled. However, 16 tablespoons would be better expressed (and easier to
measure) as 1 cup.
Write a function that expresses an imperial volume using the largest units possible. The function will take the
number of units as its first parameter, and the unit of measure (cup, tablespoon or teaspoon) as its second
parameter. Return a string representing the measure using the largest possible units as the function’s only
result. For example, if the function is provided with parameters representing 59 teaspoons then it should
return the string “1 cup, 3 tablespoons, 2 teaspoons”.
Hint: One cup is equivalent to 16 tablespoons. One tablespoon is equivalent to 3 teaspoons.
12-Magic Dates
(26 Lines)
A magic date is a date where the day multiplied by the month is equal to the two digit
year. For example, June 10, 1960 is a magic date because June is the sixth month, and
6 times 10 is 60, which is equal to the two digit year. Write a function that determines
whether or not a date is a magic date. Use your function to create a main program
that finds and displays all of the magic dates in the 20th century.
==============================================================================
file:///tmp/zim-kv6t8wsq/print-to-browser.html 3/3