1. Using a while loop, create a list
numbersthat 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.
Using a while loop, create a list called
Lthat 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
Land then increase the counter by 1. The while loop should stop once the counter variable is greater than 10.)
Using a while loop, create a list called
numsthat 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
numsand then increase the counter by 1. The while loop should stop once the counter variable is greater than 20)
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.
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.
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,
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
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.
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
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
Define a pair of functions named
generate_key(height: int, width: int, words: List[str]) -> List[List[str]] and
fill_puzzle(key: List[List[str]]) -> List[List[str]] which
generate and fill a rectangular word search puzzle of the given height and width, returning said puzzle.
You may use the
puzzle_contains_word(puzzle: List[List[str]], word: str) -> bool
function provided below to help test your functions.
Write a Python program that prepends the prefix
“pre” to every word in
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.