Thread Shed Challenge: https://www.codecademy.com/courses/learn-python-3/projects/thread-shed
Step 22 of the Thread Shed challenge has us printing out a string which includes the total of each color thread sold during the day.
My initial solution works, but since it uses an f-string, which has not actually been covered yet, I was curious as to how to do it using the methods covered so far in the course.
The video tutorial has this as the solution:
colors = ['red','yellow','green','white','black','blue','purple']
for color in colors:
print(
'Thread Shed sold {0} spools of {1} thread today.'
.format(color_count(color), colors)
)
Trying this returns a string for each item with the correct number sold but with the entirety of the “colors” list. Looking at the code, it seems to be working as expected - it’s just calling the list rather than iterating through it.
So I tried this:
#for color in range(len(colors)):
print(
'Thread Shed sold {0} spools of {1} thread today.'
.format(color_count(color), colors[color])
)
Now I have the name of each color printing as expected but the color_count function doesn’t seem to work - it returns 0 for each line. I tried troubleshooting this solution with no luck.
So my questions are:
- What am I missing with the non-f-string solutions?
- Should I be happy with the f-string solution even though it has yet to be introduced in the course? It does seem to be the most current method for such things.
Full version of my solution here:
SPOILER
daily_sales_replaced = daily_sales.replace(';,;', '@')
daily_transactions = daily_sales_replaced.split(',')
daily_transactions_split = []
for entry in daily_transactions:
daily_transactions_split.append(entry.split('@'))
# Needed a second "temp" variable in order to strip whitespace while keeping individual entries intact
transactions_clean = []
for entry in daily_transactions_split:
trans_clean = []
for item in entry:
trans_clean.append(item.strip())
transactions_clean.append(trans_clean)
customers = []
sales = []
thread_sold = []
for entry in transactions_clean:
customers.append(entry[0])
sales.append(entry[1])
thread_sold.append(entry[2])
#print(thread_sold)
total_sales = 0
for item in sales:
total_sales += float(item.strip('$'))
#print(total_sales)
thread_sold_split = []
for item in thread_sold:
thread_sold_split += item.split('&')
def color_count(color):
clr = 0
for item in thread_sold_split:
if item == color:
clr += 1
return clr
colors = ['red','yellow','green','white','black','blue','purple']
#for color in colors:
# print(
# 'Thread Shed sold {0} spools of {1} thread today.'
# .format(color_count(color), colors)
# )
#for color in range(len(colors)):
# print(
# 'Thread Shed sold {0} spools of {1} thread today.'
# .format(color_count(color), colors[color])
# )
for color in colors:
print(f'Thread Shed sold {color_count(color)} of {color} thread today.')