Chapter 17 Exercises

  1. Fix 6 syntax errors in the code below so that the code runs correctly. It will print a story.

    Change the first ' to a " in line 2. Add a " before girl in line 4. Add a " at the end of line 5. Add a + before gender in line 9. Change firstname to firstName in line 13. Add a ) at the end of line 19.

    Show Comments
  2. 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 pieces[0] and pieces[1].

    Show Comments
  3. Fix the 6 syntax errors in the code below so that it runs. It will print a story.

    Add a comma before girl on line 2. Add a " before the ) on line 5. Add a ] at the end of line 9. Add a + in line 17. Add a ( and a ) on line 23.

    Show Comments
  4. Complete the code on lines 8 - 12 to initialize the variables correctly according to the order of the input.

    Show Comments
  5. 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.

    Show Comments
  6. 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.

    Show Comments
  7. 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.

    Show Comments
  8. 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”.

    Show Comments
  9. 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.

    Show Comments
  10. 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.

    Show Comments
  11. 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.

    Show Comments
  12. 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_).

    Show Comments
  13. 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.

    Show Comments
  14. 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.

    Show Comments
  15. 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 split function. Look for getting the values out of the list.

    Show Comments
  16. 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.

    Show Comments
  17. 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.

    Show Comments
  18. 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.

    Show Comments
  19. 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).

    Show Comments
  20. 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.

    Show Comments
You have attempted of activities on this page