# 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
countdown = 100
while countdown >= 0:
print(countdown)
countdown -= 1
Run to view results
# B. Construct a string consisting of 20 "a" characters in a row
a = str()
while len(a) < 20:
a += "a"
print(a)
print(len(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 -= 10
print(price)
print(f'{price} is finally acceptable')
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
# 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 n in numbers:
print(n)
Run to view results
# B. Add each of the numbers to the sum_total variable.
sum_total = 0
for n in numbers:
sum_total += n
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
numbers = [2,3,5,8]
for n in numbers:
if n % 2 == 0:
print(f"{n} is 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 n in numbers:
if n % 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"]
['simon says ' + phrase for phrase 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]
[price * 1.15 for price in prices if price > 100]
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"]
["Gooooooooo "+player+"!" for player in players]
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 names in coffee_blends:
#print(names)
for (key,value) in names.items():
#print(value)
if key == 'name':
print(f'{value} has the following flavors:')
elif key == 'flavors':
for i in value:
print(f'- {i}')
Run to view results
#Simplified version of above
for i in coffee_blends:
print(i["name"], "has the following flavors", ', '.join(i["flavors"]))
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
for ds in store:
if total_price <1000:
#print(ds)
#print(ds["price"])
total_price += ds["price"]
purchases.append(ds['item'])
#print(total_price)
print(f'For ${total_price}, you were able to purchase the following: {", ".join(purchases)} ')
Run to view results