While loops practice checklist

1) Ask a user to choose either ‘potato’ or ‘corn.’ Keep asking until they type one of those answers, then thank them when they do so.

Example Output:

Choose either "corn" or "potato": cookies
Try again. Choose "corn" or "potato": ice cream
Try again. Choose "corn" or "potato": pizza
Try again. Choose "corn" or "potato": potato
Thank you for choosing.

2) Print the same sentence five times in a row, but only include one print command in your code. You will need to create a counter variable before your loop. Create a while loop that runs as long as the counter is below a certain number. Inside the loop, print your sentence and add one to the counter variable. (Do not use “break” in this task)

(You can do the same thing with less code using a for loop. Learning how to do this with a while loop gives you a better idea how this actually works.)

3) Using a while loop, create the following output:

# Console Output:
12
10
8
6
4

You will need to create a variable before the while loop. Print this variable inside your while loop, and also subtract from the variable inside the while loop. Write your while loop’s boolean condition to make it stop at the appropriate number. (Do not use “break” in this task)

4) Generate a random integer between 1 and 1000 and print it. Repeat this process until the random number is 7, and print how many tries it took to get a 7. See python random.

Example output:

#(many numbers displayed above)
403
260
858
173
359
654
950
540
49
7
It took 2458 tries to get a 7.

5) Create a program that repeats these steps in a while loop:

  • Ask a user to type a sentence
  • Print how many characters long the sentence is
  • Ask if they want to do another sentence

If they say “no,” quit the while loop and end the program.

Example Output:

Type a sentence: Lemons are fun.
Your sentence is 15 characters long.
Do this again (yes/no)? yes
Type a sentence: My cat is grumpy.
Your sentence is 17 characters long.
Do this again (yes/no)? no

6) Use a while loop to iterate through a list and print each item, along with its item number. This means that your loop will repeat some code for each item in the list.

Use the starter code below. Use the index variable, i, to look up item number i in the list (reminder: use square brackets).
In the while loop code block, print the item number first, then print the wood name on its own line. At the end of the program, print “Iteration Complete” once.

Your program should include exactly three print commands.

# Starter Code:
woods = ['cherry','oak','walnut','maple','pine']
i = 0
# start your while loop here
# Console Output:
0
cherry
1
oak
2
walnut
3
maple
4
pine
Iteration Complete

7) Create a game with randomized addition problems.

  • Create a random adding problem and calculate the answer in memory.
  • Check if the user got it right. Add a point to their score if they did.
  • Ask if the user wants to do another question.
  • Either quit or ask another random problem, depending on their response.

8) Consider this algebra situation:

f(x) = 2x + 1

Example: if x = 7, then f(x) = 2*7+ 1 = 15. For your next loop iteration, f(15) = 2*15 + 1 = 31, and so on.

If x starts at x = 1, how many times can you plug the old value into this equation before the result is over a million? Use a while loop to figure out the answer to this question. Print the number of times the loop has run at the end of your program.

You will need a counter variable to keep track of how many times the loop has repeated.

9) Create a total variable with a starting value of zero. Ask the user to enter a whole number and add that number to the total. Repeat this process until the total is at least 50.