# Repeating Code Using Loops

1.

```for phenotype in celegans_phenotypes:
print(phenotype)
```

2.

```for value in half_lives:
print(value, end=' ')
```

3.

```more_whales = []
for count in whales:
more_whales.append(count + 1)
```

4. a.

```alkaline_earth_metals = [[4, 9.012], [12, 24.305],
[20, 40.078], [38, 87.62],
[56, 137.327], [88, 226]]
```
b.
```for inner_list in alkaline_earth_metals:
print(inner_list[0])
print(inner_list[1])
```
c.
```number_and_weight = []
for inner_list in alkaline_earth_metals:
number_and_weight.append(inner_list[0])
number_and_weight.append(inner_list[1])
```

5.

```def mystery_function(values):
""" (list) -> list

Return a copy of the list, values, and the sublists it contains.
The top-level sublists have their elements reversed in the returned
list.

>>> mystery_function([[1, 2, 3], [4, 5, 6]])
[[3, 2, 1], [6, 5, 4]]
"""

result = []
for sublist in values:
# Copy the sublist in reverse order by inserting each
# element to the front of the new sublist.
result.append([sublist[0]])
for i in sublist[1:]:
result[-1].insert(0, i)

return result
```

6.

```text = ""
while text.lower() != "quit":
text = input("Please enter a chemical formula (or 'quit' to exit): ")
if text == "quit":
print("…exiting program")
elif text == "H2O":
print("Water")
elif text == "NH3":
print("Ammonia")
elif text == "CH4":
print("Methane")
else:
print("Unknown compound")
```

7.

```total = 0
for population in country_populations:
total += population
```

8. a.

```if rat_1[0] > rat_2[0]:
print("Rat 1 weighed more than rat 2 on day 1.")
else:
print("Rat 1 weighed less than rat 2 on day 1.")
```

b.

```if rat_1[0] > rat_2[0] and rat_1[-1] > rat_2[-1]:
print("Rat 1 remained heavier than Rat 2.")
else
print("Rat 2 became heavier than Rat 1.")
```

c.

```if rat_1[0] > rat_2[0]:
if rat_1[-1] > rat_2[-1]:
print("Rat 1 remained heavier than Rat 2.")
else:
print("Rat 2 became heavier than Rat 1.")
else
print("Rat 2 became heavier than Rat 1.")
```

9.

```for number in range(33, 50):
print(number)
```

10.

```for number in range(10):
print(10 - number, end=' ')
```

11.

```sum = 0
count = 0
for number in range(2,23):
sum += number
count += 1

average = sum / count
```

12.

```def remove_neg(num_list):
index = 0
while index < len(num_list):
if num_list[index] < 0:
del num_list[index]
else:
index += 1
```

13.

```for width in range(1, 8):
print('T' * width)
```

14.

```for width in range(1, 8):
print(' ' * (7 - width), 'T' * width, sep='')
```

15.

```width = 1
while width < 8:
print('T' * width)
width += 1

width = 1
while width < 8:
print(' ' * (7 - width), 'T' * width, sep='')
width += 1
```

16. a.

```week = 1
while rat_1_weight[week] / rat_1_weight[0] - 1 < .25:
week += 1

print(week)
```

b.

```week = 0
while rat_1_weight[week] / rat_2_weight[week] - 1 < .10:
week += 1

print(week)
```