Introduction Answers
Help Discovery Exercises
Hint
If you get stuck for a minute or more, try searching Google or using help.
If you’re stuck for more than a few minutes, some of these links might be helpful for some of the exercises below:
Multi-line Poem
Create a variable named poem that contains at least three lines of text. The poem can be one you’ve written or a poem by someone else.
When printed, your poem variable should display multiple lines of text, like this:
>>> print(poem)
The fog comes
on little cat feet.
It sits looking
over harbor and city
on silent haunches
and then moves on.
Hint
You can create multi-line strings using triple quotes (""" or ''') or by using newline escape sequences (\n) in regular strings.
Answers
Using triple-quoted strings:
>>> poem = """The fog comes
... on little cat feet.
...
... It sits looking
... over harbor and city
... on silent haunches
... and then moves on."""
>>> print(poem)
The fog comes
on little cat feet.
It sits looking
over harbor and city
on silent haunches
and then moves on.
Using newline escape sequences:
>>> poem = "The fog comes\non little cat feet.\n\nIt sits looking\nover harbor and city\non silent haunches\nand then moves on."
>>> print(poem)
The fog comes
on little cat feet.
It sits looking
over harbor and city
on silent haunches
and then moves on.
Count occurrence of word
Make a multi-line string that contains the text of Frankenstein (hint: copy-paste it from the file
frankenstein.txtthat you downloaded to yourpython_classdirectory). Note that on Windows machines, it might not copy/paste the entire file from some editors. If you have this problem, try opening the file in the browser and copying it from there.Count the number of times the string “beautiful” (with any capitalization) appears in the text (hint: dig through the string documentation)
Answers
>>> text.lower().count("beautiful")
28
Or:
>>> text.casefold().count("beautiful")
28
Note that without case-normalizing, the answer will be 27 because one instance of “beautiful” is capitalized.
Format decimal places
Make a variable
costand set it to5.5Format a string that contains a dollar sign and the value of the
costvariable, formatted to two contain decimal places (the result should be'$5.50'). We want the same formatting to work (make a correct string representing dollars and cents) if the value ofcostis7.232.
Answers
>>> cost = 5.5
>>> "${:.2f}".format(cost)
'$5.50'
>>> cost = 7.232
>>> f"${cost:.2f}"
'$7.23'
Use silly case
Alter the text of Frankenstein to make every word consist of a lower case character followed by all uppercase characters
Answers
>>> text.title().swapcase()
Count
Count the number of words in Frankenstein.
Answers
>>> len(text.split())
Count the number of lines in Frankenstein.
Answers
>>> len(text.splitlines())
Conditional Exercises
Hint
If you’re stuck for more than a few minutes, some of these links might be helpful for some of the exercises below:
Writing code at the Python REPL can be tedious at times, but it’s very handy to know how to work in the REPL. See if you can write these code blocks correctly in the Python REPL.
Temperature Conditionals
Write some code that:
sets a temperature variable
prints out “Nice and cozy” if the temperature is between 65 and 75
prints out “Too extreme” if the temperature is outside that range
Answers
>>> temperature = 80
>>> if 65 < temperature < 75:
... print("Nice and cozy")
... else:
... print("Too extreme")
...
Too extreme
Not a Robot
Write some code that:
Sets a variable
sentenceto a sentencePrints “Hi! I’m not a robot.” if the sentence includes the word “hello”
Prints “Sure, why not” if the sentence includes the phrase “can I”
Prints “I’m sorry, could you repeat that?” otherwise
Answers
>>> sentence = "Hello world"
>>> if "hello" in sentence.lower():
... print("Hi! I'm not a robot.")
... elif "can i" in sentence.lower():
... print("Sure, why not")
... else:
... print("I'm sorry, could you repeat that?")
...
Hi! I'm not a robot.
Alphabetical Ordering
Write some code that:
Sets a
my_namevariable equal to your first nameSets a
their_namevariable to the name of one of the people sitting next to youPrints “Before me in line” if their name comes before yours alphabetically
Prints “After me in line” if their name comes after yours alphabetically
Prints “We have the same name!” if your name is the same
Answers
>>> my_name = "Trey"
>>> their_name = "Diane"
>>> if their_name.lower() < my_name.lower():
... print("Before me in line")
... elif my_name.lower() < their_name.lower():
... print("After me in line")
... else:
... print("We have the same name!")
...
Before me in line
De Morgan
Rewrite this if statement to use or instead of and:
>>> user = "taylor"
>>> level = 2
>>> if user != "root" and level < 3:
... print("Permission denied")
...
Permission denied
Answers
>>> user = "taylor"
>>> level = 2
>>> if not (user == "root" or level >= 3):
... print("Permission denied")
...
Permission denied