Extra Exercises¶
- 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.
1.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.2 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)
- 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
.
2.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
.
2.2 Write a while loop that is initialized at 0 and stops at 15. If the counter is an even number, append the counter to a list called eve_nums
.
- Write a function,
sublist
, that takes in a list of numbers as the parameter. In the function, use a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the number 5 (it should not contain the number 5).
3.1 Write a function called stop_at_four
that iterates through a list of numbers. Using a while loop, append each number to a new list until the number 4 appears. The function should return the new list.
3.2 Write a function called check_nums
that takes a list as its parameter, and contains a while loop that only stops once the element of the list is the number 7. What is returned is a list of all of the numbers up until it reaches 7.
- Write a function,
sublist
, that takes in a list of strings as the parameter. In the function, use a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the string “STOP” (it should not contain the string “STOP”).
4.1 Write a function called stop_at_z
that iterates through a list of strings. Using a while loop, append each letter to a new list until the string that appears is “z”. The function should return the new list.
- Below is a for loop that works. Underneath the for loop, rewrite the problem so that it does the same thing, but using a while loop instead of a for loop. Assign the accumulated total in the while loop code to the variable
sum2
. Once complete, sum2 should equal sum1.
5.1 Below, we’ve provided a for loop that sums all the elements of list1
. Write code that accomplishes the same task, but instead uses a while loop. Assign the accumulator variable to the name accum
.
- Challenge: Write a function called
beginning
that takes a list as input and contains a while loop that only stops once the element of the list is the string ‘bye’. What is returned is a list that contains up to the first 10 strings, regardless of where the loop stops. (i.e., if it stops on the 32nd element, the first 10 are returned. If “bye” is the 5th element, the first 4 are returned.) If you want to make this even more of a challenge, do this without slicing