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

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.

Design, implement, and test a function named monthly_calendar that takes a year and a month (as integers, in that order), and returns a list of strings that can be printed (one per line) to display the Gregorian calendar for that month, formatted exactly like the Linux cal command would.

Your function should work for any date between October 1752 and December 20000

Write a Python program that prepends the prefix “pre” to every word in word_list. Modify word_list in place, do not make a new list. Print the modified word_list when done.

(As an example, if a word were ‘work’ then its replacement would be ‘prework’.)

Your code should work regardless of the values in word_list (i.e. no hard coding). This problem is worth 2 points.

Write a function called num_heads(h, f, x) that uses simulation to estimate the probability of getting exactly h heads in f coin flips. The simulation should run x trials consisting of f coin flips each. Count the number of trials (out of x) where the coin landed on heads exactly h times out of the f flips, and use this to estimate the probability. For example, num_heads(3, 10, 1000) would estimate the probability of flipping a coin 10 times and getting exactly 3 heads (and therefore, 7 tails). Ten coin flips would be simulated 1000 times. If 104 of these resulted in getting exactly 3 heads, then your function would return the estimated probability, 104/1000 = 0.104. Note that the return value will not be exactly the same each time.

You may want to make use of helper functions.

Professor Rosen suggests we play a betting game.

We are going to take a deck of cards shuffle it and cut it (split it) into three piles at random.

Professor Rosen then bets that a royal card (jack, queen, king) will be at the top of at least one of the piles. If one of the cards at the top of any of those three piles is royal, Professor Rosen wins the bet, otherwise you win the bet.

Professor Rosen helpfully explains that there are a total of 12 royal cards in a standard 52 card deck, so that’s 12 winning cards versus 40 losing cards, so the odds must obviously be in your favor.

However, you remember Professor Rosen seems to know quite a bit of math and is a bit of a trickster.

What are the real odds? Calculate them using a monte-carlo simulation.

3 points: shuffling the deck

7 points: taking a shuffled deck and splitting it into three piles (the piles can be any size but must each have at least one card)

5 points: selecting the 3 top cards

5 points: determining who won

5 points: simulating this 100000 times

5 points: calculating the real odds

(Adapted from “How to Cheat at Everything” by Simon Lovell)

The following program is meant to simulate a sequence of coin flips. The goal of the program is to simulate enough coin flips until there have been at least 100 heads and 100 tails. However, there are errors in the program on exactly 3 lines of the code. Fix those 3 lines (and only those 3 lines) so that the program behaves as it should.

Notes:
  • With the given errors the code contains an infinite loop. Debugging a program with an infinite loop is an important skill for every programmer.

  • The given code uses the choice() method from the random module. If you want to learn more about that method you can search the documentation for the random module (use the python module index).

Write a program that does the following:
  1. Generates a random integer between 1 and 100 (inclusive).

  2. Prompts the user to guess the number.

  3. Uses a while loop to repeated ask the user to guess again until they guess correctly. Give the user a hint each time by telling them if their incorrect guess is too low or too high.

  4. Print a message to the user congratulating them on guessing correctly and telling them how many guesses it took them.

Notes:
  • The setExecutionLimit is set to allow the program to run for 2 minutes (i.e. 120000 milliseconds). Feel free to modify that time limit if you want.

  • Be sure that you generate only one random number each time the program executes. It would be unfair to generate a new random number after each guess.

  • To be eligible to earn full credit you must use meaningful identifiers for your variables, and include appropriate comments in your code describing what blocks of your code are doing.

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

Notes:

  • The program should stop as soon as either turtle leaves the screen. Don’t let the program run until both leave the screen.

  • Make sure the turtles alternate their movements. Don’t have one turtle do all of its walking before the other starts.

To be eligible to earn full credit:

  1. Use meaningful identifiers for your variables.

  2. Include appropriate comments in your code describing what blocks of your code are doing.

You have attempted of activities on this page