#🔒 help with understanding code ( was a test question, test had ended)
8 messages · Page 1 of 1 (latest)
@wary pasture
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Below was the question:
b) We have six customers looking to subscribe to a single streaming service each,
as shown in the list below:
customers = [['Angelica', 'Sports', 'Reality'],
['Eliza', 'Sitcom', 'Drama'],
['Alex', 'Drama', 'Sports'],
['Peggy', 'Sitcom', 'Reality'],
['George', 'Sports', 'Film'],
['Andy', 'Reality', 'Sports']]
So, in this case, Angelica is looking to subscribe to ‘Sports’ and ‘Reality’
programs while Eliza is looking to subscribe to ‘Sitcom’ and ‘Drama’.
Create a nested list called subscriptions and for each customer, find the one
streamer that offers the cheapest prices for the two genres that they are looking
for. Then append the customer’s name, chosen streamer, and the cost of that
streamer for the two genres to the subscriptions nested list.
e.g. [[‘Angelica’,’DurianTV+’,6.0], [‘Eliza’,’CompositeVideo’,9.2], …]
Print out the entire list with a heading.
Note: The total cost for the two genres will never be more than $20.00
expected output:
subscriptions = [['Angelica', 'DurianTV+', 6.0], ['Eliza', 'CompositeVideo', 9.2], ['Alex', 'DurianTV+',
10.399999999999999], ['Peggy', 'Risney+', 5.35], ['George', 'Metflicks', 10.15], ['Andy',
'DurianTV+', 6.0]]
my code that i wrote was
#PART A
with open(file_path,"r") as f:
for line in f:
Streamer,Sports,Sitcom,Drama,Reality,Film = line.strip().split(',')
streamer_prices_list.append([Streamer,Sports,Sitcom,Drama,Reality,Film])
print("Streamer prices:")
print(streamer_prices_list)
#PART B
price = 0
customers = [['Angelica', 'Sports', 'Reality'],
['Eliza', 'Sitcom', 'Drama'],
['Alex', 'Drama', 'Sports'],
['Peggy', 'Sitcom', 'Reality'],
['George', 'Sports', 'Film'],
['Andy', 'Reality', 'Sports']]
subscriptions = [0, 0, 0] #name, streamer, sum cost #shouldnt have enterered info in to list
for customer in customers:
name, genre1, genre2 = customer
min_cost = float("inf")
chosen_streamer = ""
for row in streamer_prices_list: #should have used range(1, len(streamer_prices_list))
Streamer, cost1, cost2, _, _, _ = row #should have map(float, streamer_prices_list[row][1:])
total_cost = cost1 + cost2
if total_cost < min_cost:
min_cost = total_cost
chosen_streamer = Streamer #should have chosen_streamer = streamer_prices_list[row][0]
subscriptions.append([name, chosen_streamer, min_cost])
i have two questions to ask, firstly, what is wrong with my code, secondly, if part b was out of 30, would i be able to pass that section in your opinoin
the file below is streamer_prices.txt
Streamer,Sports,Sitcom,Drama,Reality,Film
Metflicks,7.25,3.50,9.10,6.75,2.90
DurianTV+,4.80,8.15,5.60,1.20,7.90
Risney+,6.40,2.10,9.75,3.25,8.60
CompositeVideo,5.70,1.90,7.30,4.50,9.00
Thank you very much, opinion would be much appreciated
@wary pasture
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.