Chapter 13 Exercises¶

1. Fix 7 problems in the code below so that it runs.

Add : at the end of line 2. Indent lines 3, 5, and 7. Add a " after the ( on line 5. Add a : on line 6. Add a ) at the end of line 7.

2. Fix the errors in the code so that it prints “Less than 5” when a number is less than 5 and “Greater than or equal to 5” when it is greater than or equal to 5.

Fix indentation on lines 3 and 4. Add colons after the if statements. Fix the conditions to match the words.

3. Fix 6 errors in the code below so that it works correctly.

Add a " after the ( on line 1. Change line 2 to print. Add a " before the ) on line 3. Add a : at the end of line 4. Indent lines 8 and 12.

4. Complete the code to get user input, and make choices based off the input. The input should either be “in”, “left”, or “right”; make sure the user knows that.

Complete the question asking for user input and complete the if statements.

5. Fix the code below to assign grades correctly using elif and else. You can assume the numbers are all correct.

Change lines 4, 6, and 8 to elif. Change line 10 to else:.

6. The following code prints both statements, change it so that it only prints the first one when the age is less than 6.

Use elif clause for the second one.

7. Change the code below to use elif and else rather than several ifs. Also fix it to print “Good job!” if the score is greater than 10 and less than or equal to 20 and “Amazing” if the score is over 20.

Change line 4 to elif score > 10 and score <= 20: and change line 6 to else:.

8. Complete the code so that it iterates through the list of numbers and prints positive, negative, or neither based off the integer.

Should check if x is greater than, less than, or equal to 0, and print the appropriate response.

9. Change the code below to use elif and else.

Change lines 4, 6, and 8 to elif and change line 10 to else.

10. Fix the errors in the code and change it to use elif’s and else so that if the user’s score is greater than the high score, it prints “Good job!”, if it’s lower, print “Try again.”, and if it’s the same print “You tied the high score”.

Fix indentation and add colons after the if clauses. Add quotes to strings, lowercase “input”, and fix the signs in the conditionals. Also replace the second and third conditions with elif and else.

11. Change the following code to use elif and else instead.

Change line 5 to elif and line 7 to else.

12. Add statements to the code, so that if the user gives a number less than 5, you ask for the input again, and have another set of decision statements based off if the number is greater than, less than, or equal to 3.

Complete as shown below.

13. Change the code below to use only 1 if, 1 elif, and 1 else.

Combine the first 2 if statements by using an or. Change the third if statement into an elif.

14. Fix the code and change the statements so there are three sets of if and else and 2 elifs.

15. Change the code below into a procedure that takes a number as a parameter and prints the quartile. Be sure to test each quartile.

Create the procedure and pass the number. Test the procedure for each quartile.

16. Fix the code so that it prints only 1 thing for each age group and uses elif and else.

Flip the order of the statments, and substitute elif and else clauses for everything but the first statement.

17. Write a function that will take a number as input and return a fortune as a string. Ask the user to pick a number to get the fortune before you call the function. Have at least 5 different fortunes. Use if, elif, and else.

Define the function as shown below and be sure to test it.

18. Write a function that takes in a list of grades and returns the letter grade of the average (A is 90+, B is 80-89, C is 70-79, D is 60-69, F is 59 and below). Call the function and print the result.

Iterate through the list to find the average then use if, elif, and else statements to determine the letter grade.