Follow the directions in the code. Use the Printing out a grocery list
code as a template to write your for loop
- Copy this code into your project
- Add your own grocery item to the list
- Run the code to see if your grocery item gets printed out
# Printing your grocery list
groceries = ["apples", "carrots", "onions"]
for grocery in groceries:
print("I need to get: " + grocery)
- Copy this code into your project
- Notice that this code will error when you run it because the variable
sum_of_numbers
doesn't exist - Follow the instructions in the code to add this variable.
# Adding numbers together
numbers = [2, 6, 8]
# **Create** the variable sum_of_numbers and set it to 0
for number in numbers:
sum_of_numbers += number
- Copy this code into your project.
- Run the code see what it prints out.
- Change the code so that it doesn't just print out the numbers 0 through 9, but prints out 1 through 10
# Counting to 10
for counter in range(0, 10):
print(str(counter))
Notice that "11" does not get printed out, even though 10 + 1 would equal 11. This means that counter never actually equals 10, even though in the range we say (0, 10)
- Copy this code into your project.
- Run this code and see what happens
for character in "supercalifragilisticexpialidocious":
print(character)
- Copy this code into your project.
- Edit the
reverse
function to loop over the variablestring_to_reverse
and reverse the characters.
def reverse(string_to_reverse):
reversed_string = ""
# your code goes here
reversed = reverse("abcdefghijklmnopqrstuvwxyz")
print(reversed)