Week 1 - Exercises¶
Exercise 1 - Printing multiple lines¶
Using a single print() statement, print this well-loved Shakespeare lyric so that each line is printed on a separate line, with a space between each line.
All the world’s a stage,
And all the men and women merely players:
They have their exits and their entrances;
And one man in his time plays many parts.
HINT - \ and '''
[1]:
# Put the text here
text = '''All the world’s a stage,\n
And all the men and women merely players:\n
They have their exits and their entrances;\n
And one man in his time plays many parts.'''
print(text)
All the world’s a stage,
And all the men and women merely players:
They have their exits and their entrances;
And one man in his time plays many parts.
Exercise 2 - Fixing bad code¶
The code below should have the following output:
Leave house
Go to shop
Put bread in trolley
Put milk in trolley
Put soap in trolley
Put tin foil in trolley
Put dishwasher tablets in trolley
Go to checkout
Go home
But if you try running it you will see that there are errors. Fix the code so that its output is the same as above. Don’t forget to add some comments to remind yourself what you changed!
[2]:
do_shopping_today = True # OK, I'll do it
shopping_list = ['bread', 'milk', 'soap', 'tin foil', 'dishwasher tablets'] # Quotes for soap
if do_shopping_today:
print('Leave house')
print('Go to shop')
for item in shopping_list:
print(f'Put {item} in trolley') # Correct indentation
print('Go to checkout')
print('Go home') # Closed quote
Leave house
Go to shop
Put bread in trolley
Put milk in trolley
Put soap in trolley
Put tin foil in trolley
Put dishwasher tablets in trolley
Go to checkout
Go home
Exercise 3 - A more realistic pen¶
Here’s that Pen example again…
If you managed to solve Exercise 1, try printing the solution with the Pen.write_something() instead. You should see a message saying 'Not enought ink!.
Refresh your memory on how len() works, and then without changing the ink_left attribute, see if you can adjust the code so that the ink is depleted more slowly, but still in proportion to the length of the message.
Bonus exercise - At the moment, whitespace and new line characters (\n) use ink, which is not how real pens work. Can you alter the code so that whitespace does not contribute to ink depletion? This is a tricky one…
[52]:
# Define the Pen class
class Pen:
def __init__(self):
# Attribute to keep track of ink
self.ink_left = 100.
# Method for writing stuff
def write_something(self, what):
valid_chars = ''.join(what.replace('\n', '').split(' ')) # Remove newlines and whitespace
if (len(valid_chars) * .01) > self.ink_left:
print("Not enough ink!")
else:
print(what)
self.ink_left = self.ink_left - (len(valid_chars) * .01) # Deplete 100 times more slowly
# Create an instance of Pen
my_pen = Pen()
# Insert the text from exercise 1 here
msg = '''All the world’s a stage,\n
And all the men and women merely players:\n
They have their exits and their entrances;\n
And one man in his time plays many parts.'''
# Call the write_something method
my_pen.write_something(what=msg)
# Check ink left
print(f"my_pen has {my_pen.ink_left} ink left")
All the world’s a stage,
And all the men and women merely players:
They have their exits and their entrances;
And one man in his time plays many parts.
my_pen has 98.77 ink left