P41 traverse the entire list

4-1 Come up with at least three pizzas you like, store their names in a list, and use a for loop to print out the names of each pizza.

  1. Modify this for loop so that it prints sentences that include the name of the pizza, not just the name of the pizza. For each type of pizza, a line of output such as "i like pepperoni pizza" is displayed.
  2. Add a line of code at the end of the program, outside of the for loop, that says how much you like pizza. The output should contain a message for each type of pizza, and a concluding sentence like "i really like pizza!".
pizzas = ['芝士披萨', '香肠披萨', '榴莲披萨']
for pizza in pizzas:
    print(pizza)
for pizza in pizzas:
    print("i like " + pizza)
print('i really like pizza!')
芝士披萨
香肠披萨
榴莲披萨
i like 芝士披萨
i like 香肠披萨
i like 榴莲披萨
i really like pizza!

4-2 Think of at least three animals with common characteristics, store the names of these animals in a list, and then use the for loop to print out the names of each animal.

  1. Modify the program so that it prints a sentence for each animal, such as "a dog would make a great pet".
  2. Add a line of code at the end of the program that points out what these animals have in common, such as printing a sentence such as "any of these animals would make a great pet!"
animals = ['dog', 'horse', 'cat']
for animal in animals:
    print(animal)
for animal in animals:
    print(f'a {
      
      animal} would make a great pet')
print('any of these animals would make a great pet!')
dog
horse
cat
a dog would make a great pet
a horse would make a great pet
a cat would make a great pet
any of these animals would make a great pet!

Guess you like

Origin blog.csdn.net/lijiahao1212/article/details/130367412