Chapter 8 Exercises

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

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

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

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

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

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

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

  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.

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

  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.

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

  12. Fix the errors so that the code gets the average of the numbers from 1 to 10. You should get 5.5 when you are finished.

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

  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.

  15. Fix the code below so that it will take numbers as input until you enter a negative number and then will return the average of the numbers.

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

  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.

  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

  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.

  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.

Next Section - Chapter 9 - Repeating Steps with Strings