Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Jandlize a poem, and learn to apply some Python strings functions [Mu: Python 3]
- # Copyright 2021--2023 Roland Richter
- # The code below has several bugs that cause all assert()
- # statements to fail. Your task is to FIX these errors ...
- # but you may edit the code ONLY in between the ↓ / ↑ lines.
- # Let's start with a simple example:
- # FIX the spelling so that salutation is "Hello, world!"
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- salutation = "Hell0, wolrd!"
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- assert salutation == "Hello, world!", "'" + salutation + "' ≠ 'Hello, world!'"
- # If you fixed the first error, open a handbook such as
- # https://www.w3schools.com/python/python_strings.asp
- # and continue to fix bugs, until the program runs to its end.
- # Remember that you MUST NOT edit code outside the ↓ / ↑ lines!
- # FIX a horizontal rule should be 6 times '-----'
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- horizontal_rule = 42 * "-"
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- assert horizontal_rule == 6 * "-----", "horizontal_rule ≠ 6 * '-----'"
- title = " lichtung von Ernst Jandl "
- # FIX remove leading and trailing spaces from the title
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- title = title
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- assert title == "lichtung von Ernst Jandl", (
- "'" + title + "' ≠ 'lichtung von Ernst Jandl'"
- )
- def dont_compute_lenght(s):
- return 0
- # FIX use a build-in function to compute the length of the title
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- title_length = dont_compute_lenght(title)
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- assert title_length == 24, str(title_length) + " ≠ 24"
- # FIX extract the name "Ernst Jandl" from the title string
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- name = title[5:]
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- assert name == "Ernst Jandl", "'" + name + "' ≠ 'Ernst Jandl'"
- line_one = "MancHe MeiNEN"
- # FIX convert all upper case letters to lower case, and add
- # two spaces at the beginning of line_one
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓
- line_one = line_one
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑
- assert line_one == " manche meinen", "'" + line_one + "' ≠ ' manche meinen'"
- # FIX modify the function so that it does what it states in the docstring
- # ↓---------↓---------↓---------↓---------↓---------↓---------↓---------↓
- def jandlize(text):
- """Return the text with each letter converted to lower case,
- and letters 'l' and 'r' swapped. For example, the sentence
- "It was a bright cold day in April" becomes
- "it was a blight cord day in aplir"
- Arguments:
- text -- the text to be jandlized.
- """
- result = ""
- for letter in text:
- if letter == "r":
- result += "l"
- else:
- result += letter
- return result
- # ↑---------↑---------↑---------↑---------↑---------↑---------↑---------↑
- line_two = jandlize(" Rechts und links")
- line_three = jandlize(" kann man nicht verwechseln")
- line_four = jandlize(" Welch ein Irrtum")
- assert line_two == " lechts und rinks", "'" + line_two + "' ≠ ' lechts und rinks'"
- assert line_three == " kann man nicht velwechsern", (
- "'" + line_three + "' ≠ ' kann man nicht velwechsern'"
- )
- assert line_four == " werch ein illtum", "'" + line_four + "' ≠ ' werch ein illtum'"
- print("+---------+---------+---------+---------+---------+---------+---------+")
- input("FIXED You did it! Press any key to see the poem, plus some information ...")
- print(horizontal_rule)
- print(title)
- print(line_one)
- print(line_two)
- print(line_three)
- print(line_four)
- print(horizontal_rule)
- print(
- """The poem 'lichtung' by Ernst Jandl was published in his
- first volume of poems, 'Laut and Luise', in 1966.
- Original version: http://lechts.at/lichtung/
- De-jandlized poem: http://rinks.at/richtung/"""
- )
- # ----------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement