Chapter 9 Exercises¶
Fix 5 errors in code below to correctly print: “Rubber baby buggy bumpers.”
Change
Phrase
tophrase
and add a"
at the end of the line. Add a:
at the end of line 6. Add a+
in line 8. Add a)
at the end of line 10.The code currently prints the reverse of a string. Change it so that it prints the string in the correct order, but every character is separated by a space (there should even be a space between a space and the next character).
In line 3,
newString
should equalnewString + letter + " "
. The order does matter.Fix the indention on 4 lines below to correctly print the reverse of the string. It should print: “!yadhtriB yppaH.”
Change the indention on lines 4, 7, 8, and 10 as shown below.
Fix the errors in the code to correctly print the reverse of the string. It should print: “!gnirts a m’I ,kool yeH”
newString
should be initialized outside of the for loop. Inside the for loop, it should equalletter + newString
and the print statement should be outside the for loop and it should print thenewString
Fix 4 errors in the code below to correctly print the mirror of the text in phrase. It should print: “tset a si sihTThis is a test.”
Initialize
newString
to the empty string in line 2. Changel
toletter
in line 6. Add a+
in line 8. AddnewString
on line 10.The code currently prints each letter of the string twice in a row. Change it so that it prints the mirror of the string. It should print: “!rorrim a ni gnikool ekil s’tIIt’s like looking in a mirror!”
Change line 4 to be
newString = letter + newString + newString
The code below is supposed to replace all 1’s with i’s, but it is in an infinite loop. You can reload the page to stop the infinite loop. Add a line to make the code work. It should print: “This is a string.”
Add line 5 as shown below.
Fix the errors so that the code prints “I’m just a string.”
newString should not be initialized with spaces. On line 3, it should be “for letter in phrase:”. Line 4 should be “newString = newString + letter”.
The program below is supposed to encode the text in message, but it has 5 errors. Fix the errors so that it prints: “nvvg.nv.zg.nrwmrtsg.”
Change line 2 to end in
"
. Change line 3 to start the string with"
. Change line 4 to setencodeMesage
to the empty string to start. Add:
at the end of line 5. Add(
on line 8.The code currently prints “This is a striniThis is a string”. Fix the error so that it replaces every “1” with “i” and prints “This is a string”.
At the end of the body of the while loop, make sure to find the “1” again.
Rewrite the following code to create a function that takes a string and returns the reverse of the string. It should print: “!yadhtriB yppaH.”
Define the function as shown below. Call the function and print the result.
Fix the errors in the code so that it replaces the misspelled word “recieved” with the correct spelling “received”
In line 2, make sure to find the position of the misspelled word. In line 4,
str = str[0:pos] + "received" + str[pos+len("recieved"):len(str)]
.Rewrite the following code to create a function that takes a string and returns the mirror of the string. It should print: “!ssalC iHHi Class!”.
Define the function as shown below. Call the function and print the result.
Complete the code to change all the periods to commas.
On line 2, use the find method to find the period. On line 3,
pos
should be greater than or equal to 0. Line 4 should be “str = str[0:pos] + “,” + str[pos+len(“.”):len(str)]” and line 5 should find the next period.Modify the code below to create a function that will that will take a message and return an encoded message. It should print: “nvvg.nv.zg.nrwmrtsg.”
Define the function as shown below and be sure to return the encoded string. Call the function and print the result.
Rewrite and fix the errors in the code to be a procedure that takes in a string and prints the reverse of the string and the mirror of the string. Make sure to call the procedure.
mirrorString
in line 2 should not be set equal to a string of a space. The for loop should be “for letter in phrase”.reverseString
should equalletter + reverseString
andmirrorString
should equalletter + mirrorString + letter
.Modify the code below to create a function that returns the decoded input string. It should print: “meet me at midnight.”
Define the function as shown below. Be sure to return the decoded message. Call the function and print the result.
Finish the code so that it prints the mirror of the string with the correct way then the reverse. It should print: “This is a mirror!!rorrim a si sihT”
Create another function that encodes a string. Pass in both the string to be encoded and the string to use to encode the string as well.
Create a function similar to the encode function above, but change it to take two input values.
Here’s the code to encode a message. Write code underneath it to decode the encoded message and print it.
Initialize decodedMessage and then iterate through encodedMessage and in the body of the for loop, just switch eStr and str from the encoded message.