3.8 Iterations Homework in Python
Complete the 4 hacks down below as homework:
- Uncomment the base code so that you can work on adding what you need to produce the correct outputs
- Make sure you connect too the python kernal to display an output
- Some questions require you to fill in the blanks. Beware of the ‘_’ that you need to delete and replace with numbers or words
- Make sure everything is re-indented properly before you run the code in the notebook
- Have fun!
Hack #1: Write a for loop that prints the numbers 1 through 10.
# ___ i in range(_,_):
# print(i)
Hack #2: Write a while loop that prints the even numbers between 2 and 20.
Hint 1 for Hack #2: You need to remember your work with math equations. (What goes before the equal sign?)
Hint 2 for Hack #2: You need to remember variables. (What is the initial condition?)
# ___ = __
# while num _= __:
# print(num)
# num += __
Hack #3: Shapes! Make the following shapes using iteration loops with the symbol *
This might be a little challenging but think about how you need your outputs to look.
# 1. Right triangle (Don't uncomment this, they are instructions)
# ___ i in ___(1, 6):
# print("*" * ___)
# 2. Square (5x5) (Don't uncomment this, they are instructions)
#___ i in ___(5):
# print("*" * ___)
Hack #4: Fruits! Choose your favorate fruits and list them using iterations
Pick you 3 favorte fruits and list them in the blanks
# fruits = ["___", "___", "___"]
#for ___ in ___s:
# print(___)
fruits = ["apple", "banana", "cherry"]
# ___ = 0
# while i < ___(fruits): # Hint: Loop continues as long as i is less than the length of the fruits list (The length is 3)
# print(fruits[___]) #` Print the fruit at index i
# ___ += 1