Chapter 13 Exercises¶
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.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.
Fix 6 errors in the code below so that it works correctly.
Add a
"
after the(
on line 1. Change line 2 toprint
. Add a"
before the)
on line 3. Add a:
at the end of line 4. Indent lines 8 and 12.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.
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 toelse:
.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.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 toelse:
.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.
Change the code below to use
elif
andelse
.Change lines 4, 6, and 8 to
elif
and change line 10 toelse
.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.
Change the following code to use
elif
andelse
instead.Change line 5 to
elif
and line 7 toelse
.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.
Change the code below to use only 1
if
, 1elif
, and 1else
.Combine the first 2
if
statements by using anor
. Change the thirdif
statement into anelif
.Fix the code and change the statements so there are three sets of if and else and 2 elifs.
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.
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.
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
, andelse
.Define the function as shown below and be sure to test it.
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.
Write a procedure to tell an interactive story and let the user choose one of at least 3 options.
Create a procedure as shown below. Call it to test it and print the result.
Write code that iterates through number 1 - 20 and prints “Fizz” if it’s a multiple of 3, “Buzz” if it’s a multiple of 5, “FizzBuzz” if it’s a multiple of 3 and 5, and the number if it’s not a multiple of 3 or 5. It should only print one statement per number.
Order of the first statement is important. Look below.