Python Loops
Use these exercises to practice working with conditional statements, while
loops, and Lists in Python.
Must Haves
- Create a new folder, initialize a git repository
- Create a separate file for each section (file names are listed per section)
While Loop
Save these in a file called loops.py
.
Start/End
- Ask the user for a starting number, assign it to a variable.
- Ask the user for an ending number, assign it to a variable.
- Write a loop that increments the starting number by
1
until it matches the ending number.
Solution
Multi-Condition Check
- Create string with at least 2 words and at least 16 characters total.
- Create a variable called
counter
with a starting value of0
- Loop through the string
- Increment the
counter
by1
- Stop the loop when
counter
is greater than the length of your string.
- Increment the
- IN THE LOOP
- Write a condition to see If the
counter
value is an even number. - Also make sure the value of your string at the index of
counter
isn't a blank space! - If both of those conditions are met, print the value of your string at that counter position
- Write a condition to see If the
- The result should be only letters that occur in even numbered indexes.
Solution
Bonus
- Can you write a loop that increments by more than
1
? - Can you create a range of numbers from which the user can choose?
- Can you let the user know when they choose something out of that range?