14.7. ExercisesΒΆ

  1. 1. Using a while loop, create a list numbers that contains the numbers 0 through 35. Your while loop should initialize a counter variable to 0. On each iteration, the loop should append the current value of the counter to the list and the counter should increase by 1. The while loop should stop when the counter is greater than 35.

    Show Comments
  1. Using a while loop, create a list called L that contains the numbers 0 to 10. (i.e.: Your while loop should initialize a counter variable to 0. On each iteration, the loop should append the current value of the counter variable to L and then increase the counter by 1. The while loop should stop once the counter variable is greater than 10.)

  1. Using a while loop, create a list called nums that contains the numbers 0 though 20. (i.e: your while looop should initialize a counter variable on 0. During each iteration, the loop should append the current value of the counter variable to nums and then increase the counter by 1. The while loop should stop once the counter variable is greater than 20)

  1. Modify the walking turtle program so that rather than a 90 degree left or right turn the angle of the turn is determined randomly at each step.

  1. Modify the turtle walk program so that you have two turtles each with a random starting location. Keep the turtles moving until one of them leaves the screen.

    Show Comments
  1. Create a while loop that initializes a counter at 0 and will run until the counter reaches 50. If the value of the counter is divisible by 10, append the value to the list, tens.

  1. Use a while loop to iterate through the numbers 0 through 35. If a number is divisible by 3, it should be appended to a list called three_nums.

14.7.1. Contributed ExercisesΒΆ

Q-1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?

While loops are commonly used to validate user input. Write code that asks users to enter their age. If they enter a value less than 0 or more than 120, they should be reprompted again until they enter a valid value. When a valid answer is entered, print a message displaying the person’s age.

The input function should only be called on one line of code in your program.

Write a for loop to print out the contents of the majors list, one at a time. Below it, write a second loop, a while loop, to do the same thing.

(8 points)

The generate_list() function should return a list with every integer between 1 and 10, including 1 and 10. The code has errors. Fix the errors so it works correctly.

This question has no unit tests.

The answer to the question, “How far can a stack of n identical books overhand the edge of a table without falling off?” is given by: d=0.5*sum(1/n) as you sum from 1 to n. Where d is the distance from the edge of the table to the outside edge of the top book. Use a while loop to figure out the number of books(n) that are needed to have a book length over hang (aka d>1).

Request a user input an string s, use a while loop to keep looping until the value entered contains the word “stop” or “end”. This should re-ask each loop.

Use turtle (and the coding example in the reading…) to draw a random turtle path on screen. Modify the code so that the turtle stops if it goes outside of the area described by a circle of a given radius. The code you wrote for the monte carlo simulation might be helpful…

Create a while loop that randomly generates numbers until the sum of the numbers sum_rand is greater than 100. Create a histogram using Altair with all the random numbers found.

You have attempted of activities on this page
Next Section - 14.8. Chapter Assessment