# Use while loops to solve the following challenges
Run to view results
# A. Count down from 100 to 0 with a loop, including a print statement
x = 0
while x < 101:
x += 1
print(x)
Run to view results
# B. Construct a string consisting of 20 "a" characters in a row
x = 0
list_a = []
while x < 21:
x += 1
list_a.append("a")
print(list_a)
Run to view results
# C. Bring down the price variable by increments of 10 until it is lower than the acceptable_price
price = 1000
acceptable_price = 954
while price > acceptable_price:
price -= 5
print(price)
Run to view results
Run to view results
# D. Spring cleaning: remove all instances of "mess" from the garage_contents list
# Hint: Use the in operator to check if "mess" is still in the list, and .remove() to get rid of it
garage = ["bike", "mess", "generator", "mess", "mess", "tools", "car", "mess"]
while "mess" in garage:
garage.remove("mess")
print(garage)
Run to view results
numbers = [2,3,5,8]
# Use for loops to solve the following challenges
Run to view results
# A. Print through each number in the numbers list
numbers = [2,3,5,8]
for b in numbers:
print(b)
Run to view results
# B. Add each of the numbers to the sum_total variable.
sum_total = 0
for i in range(0,5):
sum_total += i
print(sum_total)
Run to view results
# C. Print the message "even" for each number that divides evenly by 2
# Hint: The modulus symbol, %, gives you the remainder when dividing two numbers
for i in numbers:
if i % 2 == 0:
print(f"{i} is an even #")
else:
print(f"{i} isn't an even #")
Run to view results
# D. Count how many even numbers there are by adding 1 to count for each even number
even_count = 0
for i in numbers:
if i % 2 == 0:
even_count += 1
print(even_count)
Run to view results
# Use list comprehension to generate new lists for these challenges
Run to view results
# A. Add "simon says " to all strings in this list
commands = ["jump", "duck", "touch your toes"]
new_commands = [f"simon says {command}" for command in commands]
Run to view results
# B. Add 15% to each number above 100, and don't include other numbers
prices = [75, 500, 125, 20, 43]
new_prices = [price * 1.15 for price in prices]
Run to view results
# Use any variety of loop for the following challenges
Run to view results
# A. As a superfan, you want to chant each of the player's names in the following format:
# 1. Spell out their name with a separate print statement for each letter, followed by an exclamation point
# 2. Then, print the message "Goooo _______!" by inserting the full name in the blank
players = ["Anoushka", "Becky", "Mehreen"]
for player in players:
for letter in player:
print(letter + "!")
print(f"Goooo {player}!")
Run to view results
# B. Customers want to know the flavors of each coffee. Use separate print statements in the following format:
# First, print "_______ has the following flavors" using the coffee name in the blank
# Then, print each flavor inside the coffee's flavor list
coffee_blends = [
{"name": "Don Manuel", "flavors": ["cocoa", "lime", "cinnamon"]},
{"name": "Gitwe Washed", "flavors": ["grapefruit", "hibiscus", "honey"]}
]
for coffee in coffee_blends:
print(f"{coffee['name']} has the following flavors:")
for flavor in coffee["flavors"]:
print(flavor)
Run to view results
# C. Shop till you've dropped 1000 dollars!
# Add each item to the purchases list until the total price exceeds 1000
store = [
{"item": "mountain bike", "price": 375},
{"item": "tambourine", "price": 50},
{"item": "massage chair", "price": 700},
{"item": "worlds best programmer mug", "price": 10},
{"item": "new computer", "price": 1500}
]
purchases = []
total_price = 0
item_index = 0
while total_price < 1001:
purchases.append(store[item_index])
total_price += store[item_index]["price"]
item_index += 1
Run to view results