Chapter 8 Exercises¶
Fix the 5 syntax errors in the code below to print a countdown of the numbers from 10 to 0.
The following code will loop infinitely. Make one change that will make it loop only 5 times.
Make 5 changes to the code below to correctly print a count up from -10 to 0.
Fix the errors in the code below so that it uses 2 loops to print 8 lines of
*
.Finish lines 1 and 5 so that the following code correct prints all the values from -5 to -1.
Complete the code on lines 4 and 6 so that it prints the number 6.
The code below is supposed to print an estimate of the square root. But, the indention is wrong on 4 lines. Fix it.
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.
The program below is supposed to print the times tables for 1 to 3, but there are 5 errors. Fix the errors.
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.
Rewrite the following code to use a while loop instead of a for loop.
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.Rewrite the following code to use a while loop instead of a for loop.
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.
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.
Fix and change the code so it prints a table of division instead of multiplication for -10 to -1.
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.
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 equalCreate 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.
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.