Chapter 17 Exercises¶
Fix 6 syntax errors in the code below so that the code runs correctly. It will print a story.
Change the first
"in line 2. Add a
girlin line 4. Add a
"at the end of line 5. Add a
genderin line 9. Change
firstNamein line 13. Add a
)at the end of line 19.
Fix the errors so that it prints the first and last name only.
Fix the quotation mark in the second line. In the fifth line add quotations around the comma. Print
Fix the 6 syntax errors in the code below so that it runs. It will print a story.
Add a comma before
girlon line 2. Add a
)on line 5. Add a
]at the end of line 9. Add a
+in line 17. Add a
)on line 23.
Complete the code on lines 8 - 12 to initialize the variables correctly according to the order of the input.
Indent 6 lines and fix the call to the procedure so that it works correctly. It will print a story.
Indent lines 3-8. Add a
,between the first name and the last name on the call on line 18. Move
"boy"to after the last name in the call on line 18.
The input is a list of strings, add code below it (and fix indentation and variable names if needed) so that the code will print a story for each string in the list.
Add a for loop, fix the indentation to put everything besides input in the body of the for loop, and change pieces to equal
i.split(",")where i is each element in the string list.
Change 4 lines in the code below so that runs correctly without any errors. It will print a poem.
Fix the indices on lines 3-6 as shown below.
Fix the 6 errors so that the code prints the story properly.
On line 5, add quotations around the comma. On lines 8 - 11, fix the index positions. On line 15, add a plus sign before “gender”.
Turn the following code into a function. It finds the name in a string and prints it. Pass in the string and return the name if it is found and “Unknown” if not. Be sure to call the function to test it. Test it both when the name is there and when it isn’t.
Define the function as shown below. Be sure to call the function to test it. Test it both when the name is there and when it isn’t.
The procedure below takes a string and splits it to create a story. Change it so that it takes multiple strings, so it creates the story without having to use split.
Create the procedure as shown below.
Change the following code into a function that prints a crazy headline. It should take the values as parameters. Be sure to call the function to test it.
Define a function that takes a list and then call the function and pass in the list. Print the result.
Fix the 3 errors to make the code print out the age.
In the if clause, check if the
posAge > -1. In the body of the if clause, set the substring to be
posAge + 6:len(agePart_).
Change the following into a procedure that prints the following story. Pass in the values that can change.
Define the procedure and call it. Be sure to pass the name and item.
Change the following into a procedure that takes one string in the format “name: Bob, age: 10”. The procedure should print the name and age.
Define procedure as shown below.
Write a personalized story. It should start with a string of input and split that string to get the parts it needs for the story. For example, define a name, animal, animal name, and animal adjective and create a story from that.
Here is one example. Look for a string with values separated with a comma. Look for use of the
splitfunction. Look for getting the values out of the list.
Write a procedure that takes in a string with 2 adjectives, a noun, and a verb separated by a comma (ex: “crazy,blue,banana,runs”) in that order and prints a sentence using all 4 words.
Define a procedure similar to the one below.
Write a procedure that prints a personalized story. It should take as input the items that will allow you to personalize a story.
The example below is one possible procedure. Be sure to call it to test it.
Write a procedure that takes in a name, age, and 2 verbs. If the age is less than 10, print a sentence using the name, age, and first verb. Otherwise print a sentence using the name, age, and second verb.
Define a procedure similar to the one below.
Write a procedure that prints a personalized story. It should take as input the items that will allow you to personalize a story. It should also take a gender and vary the story based on the gender.
Here is one example. Look for a conditional with two options (if and else).
Create a procedure that takes in a string like “name: Bob,age: 10,verb: dance” and prints out a sentence with just the name, age, and verb.
Define a procedure similar to below.