Chapter 8 Exercises

  1. Fix the 5 syntax errors in the code below to print a countdown of the numbers from 10 to 0.

    Change Counter to counter on lines 2 and 4. Loop while counter is greater than or equal to 0. Change the Print to print. Change the last line to subtract one rather than add it.

    Show Comments
  2. The following code will loop infinitely. Make one change that will make it loop only 5 times.

    Since, x is already greater than zero, it will keep looping. Change line 4 to be x = x - 1.

    Show Comments
  3. Make 5 changes to the code below to correctly print a count up from -10 to 0.

    Change the while to less than or equal zero. Add a : at the end. Change it to x = x + 1 and move it after output = output + str(x) + " ". Indent the output = output + str(x) + " ".

    Show Comments
  4. Fix the errors in the code below so that it uses 2 loops to print 8 lines of *.

    The first loop should be from range 0 to 2 (or any two numbers that are 2 numbers apart). Line 1 should also have a colon at the end and line 2 and 3 should be indented. Line 4 and 5 should be indented 8 spaces.

    Show Comments
  5. Finish lines 1 and 5 so that the following code correct prints all the values from -5 to -1.

    Change line 1 to set output to the empty string (""). Change line 5 to x = x + 1.

    Show Comments
  6. Complete the code on lines 4 and 6 so that it prints the number 6.

    Line 4 should be x = x + 1 and line 6 should print x.

    Show Comments
  7. The code below is supposed to print an estimate of the square root. But, the indention is wrong on 4 lines. Fix it.

    Don’t indent line 2. Indent line 6 under line 5. Indent line 7 at the same level as line 5. Don’t indent line 8.

    Show Comments
  8. The function currently takes a start and stop argument and uses a for loop to find the sum of all the numbers between them (inclusive). Change the for loop to a while loop while still using the parameters.

    Initialize num to be equal to the start argument. Make the while loop check to see if num is less than or equal to the stop argument. In the body of the while loop, make sure to increment num by 1.

    Show Comments
  9. The program below is supposed to print the times tables for 1 to 3, but there are 5 errors. Fix the errors.

    Change line 1 to end the range at 4. Change line 2 to end the range at 11 and add the : at the end. Fill in the missing + between strings in line 3 and add str(x*y).

    Show Comments
  10. Rewrite the code that prints the times tables for 1 to 3 using a while loop and a for loop instead of two for loops.

    Can do in either order, but make sure the variable is being incremented for the while loop in the correct place.

    Show Comments
  11. Rewrite the following code to use a while loop instead of a for loop.

    Change line 2 to create number and set it to 1. Change line 3 to loop while the number is less than 11. Add a line before the print statement to increment number.

    Show Comments
  12. Fix the errors so that the code gets the average of the numbers from 1 to 10.

    Set sum equal to 0 and x equal to 1. Indent lines 4 and 5 and on line 4, sum should equal sum plus x. The average is equal to the sum divided by x.

    Show Comments
  13. Rewrite the following code to use a while loop instead of a for loop.

    Change line 4 to only create and initialize number. Change line 6 to loop while number is less than 21. Add a step 5 where the value of number is set to the current value plus 2.

    Show Comments
  14. The code below currently enters a loop where it keeps printing “Even”. Fix the code so that it prints “Even” and “Odd” for numbers 0 to 9.

    You have to increment the counter in the body of each inner loop.

    Show Comments
  15. Modify the code below to create a function that will take numbers as input until you enter a negative number and then will return the average of the numbers.

    Define the function. Return the sum divided by the count. Call the function and print the result.

    Show Comments
  16. Fix and change the code so it prints a table of division instead of multiplication for -10 to -1.

    Change the range in line 1 from -10 to 0 and add the colon. Fix the indentation in the rest of the lines and in line 3 change the string and operator to reflect division

    Show Comments
  17. Create a function to calculate and return the sum of all of the even numbers from 0 to the passed number (inclusive) using a while loop.

    Create the function and be sure to call it to test it.

    Show Comments
  18. Write a procedure that takes a user input and keeps asking for a user input until the input is “Hello”. If the input is not “Hello”, it should print “This is your n wrong try.” where n is the number of times they have put an input in. If they type “Hello”, the procedure should print “Success!”. Hint: != means does not equal

    In the procedure, get an input and initialize the count. Then create the while loop with the condition that the input does not equal “Hello”. Increment count first in the loop if count was initialized as 0. Increment it last if count was initialized as 1. Ask for input again in the body of the while loop.

    Show Comments
  19. Create a procedure to print stars and spaces in a roughly square pattern and have it take as input the number of stars on a side. Use a nested loop to do this.

    Create the procedure and be sure to call it to test it.

    Show Comments
  20. Write a procedure that takes an int argument and uses a while loop to create a right-triangle like shape out of *. The first row should have 1 star and the last should have n stars where n is the argument passed.

    Assign a counter variable to 1 and increment it in the body of the while loop.

    Show Comments
You have attempted of activities on this page