Practice Projects 1
Practice Projects 1
Practice Questions
1. Fix the Errors: Given the following code, fix any/all errors so that it outputs “Hello John”
correctly:
>>> name = "John"
>>> if name == "Jack"
>>> print("Hello Jack"):
>>> elif
>>>> print("Hello John"):
2. Answer the following questions
1.
What are escape characters?
2.
What do the \n and \t escape characters represent?
3.
How can you put a \ backslash character in a string?
4.
The string value "Howl's Moving Castle" is a valid string. Why isn’t it a problem that the
single quote character in the word Howl's isn’t escaped?
5. If you don’t want to put \n in your string, how can you write a string with newlines in it?
6. What do the following expressions evaluate to?
• 'Hello, world!'[1]
• 'Hello, world!'[0:5]
• 'Hello, world!'[:5]
• 'Hello, world!'[3:]
7. What do the following expressions evaluate to?
• 'Hello'.upper()
• 'Hello'.upper().isupper()
• 'Hello'.upper().lower()
8. What do the following expressions evaluate to?
• 'Remember, remember, the fifth of November.'.split()
• '-'.join('There can be only one.'.split())
9. What string methods can you use to right-justify, left-justify, and center a string?
10. How can you trim whitespace characters from the beginning or end of a string?
Practice Projects
1. Pig Latin: To form Pig Latin, you take an English word that begins with a consonant, move that
consonant to the end, and then add “ay” to the end of the word. If the word begins with a
vowel, you simply add “way” to the end of the word. One of the most famous Pig Latin phrases
of all time is “ixnay on the ottenray,” uttered by Marty Feldman in Mel Brooks’s comedic
masterpiece Young Frankenstein. Write a program that takes a word as input and uses indexing
and slicing to return its Pig Latin equivalent.
2. User Input: Ask the user to input the time of day in military time without a colon (1100 = 11:00
AM). Write a conditional statement so that it outputs the following:
a. “Good Morning” if less than 1200
b. “Good Afternoon” if between 1200 and 1700
c. “Good Evening” if equal or above 1700
3. Alter the calculator project done in class so that the order of the numbers doesn’t matter.
There are a few ways to get the same result; one way is to ask the user if they’d like to
reverse the placement of the numbers.
4. Age Group: Ask the user to input their age. Depending on their input, output one of the
following groups:
a. Between 0 and 12 = “Kid”
b. Between 13 and 19 = “Teenager”
c. Between 20 and 30 = “Young Adult”
d. Between 31 and 64 = “Adult”
e. 65 or above = “Senior”
This is an open-ended exercise. Create a text-based RPG with a story line. You take user input and give
them a couple choices, and depending on what they choose, they can go down a different path. You’ll
use several branching statements depending on the length of the story.
Use the dictionary value {'1h': 'bking', '6c': 'wqueen', '2g': 'bbishop', '5h': 'bqueen', '3e': 'wking'} to
represent a chess board. Write a function named isValidChessBoard() that takes a dictionary argument
and returns True or False depending on if the board is valid. A valid board will have exactly one black
king and exactly one white king. Each player can only have at most 16 pieces, at most 8 pawns, and all
pieces must be on a valid space from '1a' to '8h'; that is, a piece can’t be on space '9z'. The piece names
begin with either a 'w' or 'b' to represent white or black, followed by 'pawn', 'knight', 'bishop', 'rook',
'queen', or 'king'. This function should detect when a bug has resulted in an improper chess board.
Table Printer
Write a function named printTable() that takes a list of lists of strings and displays it in a well-organized
table with each column right-justified. Assume that all the inner lists will contain the same number of
strings. For example, the value could look like this:
Hint: your code will first have to find the longest string in each of the inner lists so that the whole
column can be wide enough to fit all the strings. You can store the maximum width of each column as a
list of integers. The printTable() function can begin with colWidths = [0] * len(tableData), which will
create a list containing the same number of 0 values as the number of inner lists in tableData. That way,
colWidths[0] can store the width of the longest string in tableData[0], colWidths[1] can store the width
of the longest string in tableData[1], and so on. You can then find the largest value in the colWidths list
to find out what integer width to pass to the rjust() string method.