# Noemi Cabrera
# 31 October
# In this lesson, I learned to iterate through lists using the for loop with in. A
# variable of any name can be placed after the for keyword to iterate through a defined
# list. With this for loop you can add the integres of a list, add the strings of a list,
# count the items in a list, etc.
# I had diffculties creating the last program. I didn't know how to fix the problem I
# was having with displaying the total number of bones identified. After
# researching, I found that that I had to make the variable 'identified' global so that
# it could be used many times since it was in a function.
# [ In this code, the for loop iterates through each string in the cities variable and
# assigns each one to the variable city, which can be any variable name, and then they're
# printed in a separate line.] review and run example
cities = ["New York", "Shanghai", "Munich", "Tokyo", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
for city in cities:
print(city)
# [ In this code, each of the iteams in the slaes list is added to get the total of the
# sales. The for loop contains the total variable that is added with each of the items
# until the list is empty. ] review and run example
sales = [6, 8, 9, 11, 12, 17, 19, 20, 22]
total = 0
for sale in sales:
total += sale
print("total sales:", total)
# [ The same code above is used here, however, the variable sale is changed to dollars.
# This does not affect the code. ] review and run example
sales = [6, 8, 9, 11, 12, 17, 19, 20, 22]
total = 0
for dollars in sales:
total += dollars
print("total sales:", total)
# [ In this code, each string in the list birds is printed using the for loop. ]
# create a list of 4 to 6 strings: birds
# print each bird in the list
birds = ["owl","hummingbird","crow","gold finch","cardinal"]
for name in birds:
print(name)
# In this code, the integers in the player_points list are doubled by mulitplying them
# by 2 in a foor loop. Each value is printed in a separate line.
# [ ] create a list of 7 integers: player_points
# [ ] print double the points for each point value
player_points = [5, 10, 3, 8, 4, 12, 20]
for point in player_points:
print(point*2)
# [ In this code, a long string is created by adding the names found in the birds list.
# A for ... in loop is used to iterate through each name and add it to the long_string
# variable. ] create long_string by concatenating the items in the "birds" list previously created
# print long_string - make sure to put a space betweeen the bird names
long_string = ""
for name in birds:
long_string += " " + name
print(long_string)
# [ In this code, the comparison operator < to check if the lenght of the items in the
# foot_bones list is less than 10. If true, then the item wll go to the shorter_names
# variable. If true, then the item wll go to the shorter_names variable. If false, then
# the item wll go to the longer_names variable. ] review and run example of sorting into
# strings to display
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
longer_names = ""
shorter_names = ""
for bone_name in foot_bones:
if len(bone_name) < 10:
shorter_names += "\n" + bone_name
else:
longer_names += "\n" + bone_name
print(shorter_names)
print(longer_names)
# [ This code is simlar to the one above, but this time the items in the foot_bones list
# are placed into lists with the .append() method.] review and run example of sorting
# into lists
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
longer_names = []
shorter_names = []
for bone_name in foot_bones:
if len(bone_name) < 10:
shorter_names.append(bone_name)
else:
longer_names.append(bone_name)
print(shorter_names)
print(longer_names)
# In this code, the list cities used in one of the cells above is used. If some items
# start with m, then those items are printed. If they do not start with m, they're not
# printed.
# [ ] Using cities from the example above iterate throught the list using "for"/"in"
# [ ] Print only cities starting with "m"
for city in cities:
if city.lower().startswith("m"):
print(city)
else:
pass
# [ ] Using cities from the example above iterate throught the list using "for"/"in"
# cities = ["New York", "Shanghai", "Munich", "Tokyo", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
# [ ] sort into lists with "A" in the city name and without "A" in the name: a_city & no_a_city
a_city = []
no_a_city = []
for city in cities:
if city.lower().startswith("a"):
a_city.append(city)
else:
no_a_city.append(city)
print(a_city)
print(no_a_city)
# [ In this code the count method is used to count the number of times the letetr a is
# found in the cities list. The for loop iterates through each city and the the count of
# the letter a is stored in a variable until there are no more a's. The total sum is
# printed.] review and run example
# iterates the "cities" list, count & sum letter "a" in each city name
cities = ["New York", "Shanghai", "Munich", "Tokyo", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
search_letter = "a"
total = 0
for city_name in cities:
total += city_name.lower().count(search_letter)
print("The total # of \"" + search_letter + "\" found in the list is", total)
# [ In this code, the user is asked to enter a visited city name. If that name is found,
# then a statement saying so will display. If that name is not found, then a statement
# saying so will display. The city_search function is defined and takes 2 arguments
# This is used to search the city entered by the user in the default and the
# visited_cities list.] review and run example
# city_search function has a default list of cities to search
def city_search(search_item, cities = ["New York", "Shanghai", "Munich", "Tokyo"] ):
for city in cities:
if city.lower() == search_item.lower():
return True
else:
# go to the next item
pass
# no more items in list
return False
# a list of cities
visited_cities = ["New York", "Shanghai", "Munich", "Tokyo", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
search = input("enter a city visited: ")
# Search the default city list
print(search, "in default cities is", city_search(search))
# search the list visited_cities using 2nd argument
print(search, "in visitied_cites list is", city_search(search,visited_cities))
# [ In this program, the user is asked to enter a paint color to see if it's available.
# If the color is in the list of paint colors defined, then a statement saying it's
# found is displayed. If not, then a statement saying it's not found is displayed ]
# complete paint stock
def paint_search(search_color, paint_colors = ["pink","blue","purple", "turquoise", "white"] ):
for color in paint_colors:
if color == search_color.lower():
return "color found"
else:
pass
return "color not found"
color_request = input("Enter the paint color you need:").lower()
print(color_request, paint_search(color_request))
# [ This program is designed to be a quiz that identifies if the foot bone entered is
# found in the foot_bones list. If the bone is not found, then incorrect displays.
# If the bone is found in the list, correct displays. The bone_search function decides
# if answer is correct or incorrect and identifies how many times the answers were
# correct. ] Complete Foot Bones Quiz
foot_bones = ["calcaneus", "talus", "cuboid", "navicular", "lateral cuneiform",
"intermediate cuneiform", "medial cuneiform"]
identified = 0
def bone_search(search_bone):
global identified
for bone in foot_bones:
if bone == search_bone.lower():
identified += bone.count(bone)
return "correct"
else:
pass
return "incorrect"
print("Is talus in the foor_bone list?:", bone_search("talus"))
print("Is distal in the foor_bone list?:", bone_search("distal"))
print("Total number of identified bones:", identified)
# [ ] bonus version