Chapter 12 Exercises¶
Fix 3 syntax errors in the code below so that it correctly prints “x is less than 3” and then “All done” when x is less than 3.
:at the end of line 2. Indent line 3. Add a
)in line 4.
The code currently prints “Hello” when the number is less than 3. Change it so that it prints “Hello” when the number is equal to 3.
Change the less than sign to
Fix the indention in the code below to use a price of 1.45 if the weight is less than 1 and a price of 1.15 otherwise. There is also one syntax error.
Change the indention on lines 3 and 5 and add a
:at the end of line 4.
Fill in line 1 with a weight that will make the total equal 1, and fix the indentation errors.
The weight should be .5, and the indentation should look like below.
Fix 3 errors with indention in the code below to correctly set the price to 1.5 if the weight is less than 2 and otherwise set it to 1.3.
Indent lines 4 and 6. Remove the indention on line 9.
The code currently does not do anything if the number is equal to 2. Fix it so that it prints “Hey” if the number is 2.
Make the second clause
Fix 4 errors in the code below to print “x is a number from 1 to 10” when x is greater than or equal to 1 and less than or equal to 10.
Change line 2 to
x >= 1and add a
:at the end of the line. Indent line 3. Remove the indent on line 4.
The following code prints “This is not 8” when the number is not 8. Change the if statement, so that it does the same thing but only uses one expression (No “and” or “or”).
Change it to
x != 8.
Finish the conditional on line 3 to print “You can go out!” if either cleanedRoom or finishedHomework is true (not 0). It should always print “All done” as well.
cleanedRoom or finishedHomework:to line 3.
Complete the conditional and fix the errors so that it prints “Good job” when the number is between 1 and 10 (inclusive) or is 15 and “Fail” when it is not.
Switch the indentation levels of lines 2 and 3, and use the conditional below.
Fix 5 errors in the following code to set the price to 1.45 if the weight is less than or equal to 1 and otherwise set it to 1.15.
Indent lines 3 and 5. Change line 2 to
<=. Change line 7 to
weight. Change line 8 to
Fix the indentation below, so that the code sets the price based off the weight, then checks if the total is greater than the amount in your wallet.
Fix the indentation so it looks like below.
Change 3 lines in the code below to correctly set the grade so that a 90 and above is an A, 80-89 is a B, 70 - 79 is a C, 60-69 is a D and below 60 is an E.
Change lines 4, 6, and 8 as shown below.
Fix the errors in the code, and change it, so there’s only 1 if statement. The code should print “The number is 5” when the number is 5, and should print “The number is NOT 5” when it is not.
Fix the indentation and change the second if statement to an else statement.
Fix 5 errors in the following code to set price to 1.45 if weight is less than 1 and otherwise set it to 1.15.
:at the end of lines 2 and 4. Indent lines 3 and 5. Add a
)at the end of line 8. Change line 9 to
Complete and finish the code on lines 1 and 4 so that the code prints “Hi”.
Line 1 should be x = 3, and line 4 should be else.
Write a procedure that will print out “even” if the passed value is even and “odd” if the passed value is odd. Test both possibilities.
Define the procedure as shown below and be sure to test it.
Write a procedure that takes 2 ints, total price, and amount in wallet. Print “You have enough money” if the difference between the wallet and price is 0 or greater; otherwise, print “Get more money”
Do as shown below.
Write a function that takes a number for a grade and returns a string grade. It should return E for any value below 60, D for 61 to 69, C for 70 to 79, B for 80 to 89 and A for 90 and above. Write code to test each grade range.
Create a function as shown below. Call it to test it and print the result.
Write code that prints “Fizz” when the number is divisible by 3, “Buzz” when it is divisible by 5, and “FizzBuzz” when it is divisible by 3 and 5 (If a number is divisible by 3 and 5, it should also print “Fizz” and “Buzz”).