Python Fundamentals Practice Problems: Laptop Purchase - Alternative project

I have a few questions related to the alternative mission I invented in Practice Mode :smiley:

It took two days to write it. I learned a lot in this time. ( More story inside my code).

My main one is: why in output I see dollars fonts after the name of laptops ?
Second: what could be written simpler?

Practice problems: Dictionaries Mission Practice Problems: proble n. 12 (“Laptop Purchase”)

My output:

My whole code below:

# So, I spontaneously decided to make a project from this exercise. 
#The reason was: I'm the person who likes the best gear, hardware. 
#So, the idea was to find the most expensive laptop for me and the cheapest one: 
#for my cat ( she likes to see birds on youtube ). 
#Laptop for my cat should only be able to handle youtube. 
#After 2 days of thinking around it, finally I made it. 
#There are some flaws ( I hope the community will help me with them - I'm just beginner)


from csv import reader
opened_file = open('laptops.csv')
read_file = reader(opened_file)
list_file = list(read_file)
list_head = list_file[:1]
laptops = list_file[1:]
# print(laptops[:][1])
price_to_name = {}

for row in laptops:
    price = int(row[2])
    label = row[1]
    if price in price_to_name:
        price_to_name[price] += label
    else:
        price_to_name[price] = label

# To find the most expensive laptop I decided to create two lists 
#( for the most expensive with decreasing prices and for the less expensive - opposite).

minimum = 0
maximum_list = []
minimum_list = []

for x in price_to_name:
    # The most expensive laptop:
    if x < 5000 and x > 3500:
        maximum = x
        maximum_list.append(maximum)
        maximum_list.sort(reverse=True)
        
"""        
# Test: maximum_list -> Output:

# print('maximum_list =', maximum_list) 
"""
# I don't know why it printed only 3 values, I have some assumptions only 
#( I noticed that this version of code enhances the speed of computing). 
#Anyway, I assumed that it will print all of them in reversed mode. 
#Despite that error, I have enough data to continue.

# "The cheapest laptop" part:
for x in price_to_name:
    if x > 0  and x < 1900 and x > minimum:  
        minimum = x
        minimum_list.append(minimum)
        minimum_list.sort()
"""
# Test: minimum_list -> Output        
        
# print("\n")       
# print('minimum_list =', minimum_list)
"""

# Now I need to compare both lists and compute: what's the best offer:

laptop1_price_key = None
laptop2_price_key = None    

# My first assumption is that I can spend all money: 5000 $, but, I can't. 
#Output is: "None" - so it's impossible to spend all the money. 
#Because of that I included this as a variable in the code: a special printed message.

for value_x in maximum_list:
    x = value_x
    for value_y in minimum_list:
        y = value_y
        best_offer = 5000  
        if (x + y == best_offer) and (x != y) and (x and y != None):
            laptop1_price_key = x
            laptop2_price_key = y    
        else:
            #below build filter that is searching for any answer where I can get one premium laptop for me 
            #and one crappy one( for my cat):
            for i in range(5000, -1, -1):
                if (laptop1_price_key or laptop2_price_key) == None and (x + y == i) and x != y:
                    best_offer = i
                    laptop1_price_key = x
                    laptop2_price_key = y
                    if (laptop1_price_key and laptop2_price_key) != None:
                        break
    

print("Sorry, but it is not possible to find two laptops which will cost 5000$ in total (where one is fine, and one, not so much). The best available solution ( 'one premium laptop for you and one crappy one, for the cat' ) is:")

print("\n")

for laptop_for_me in price_to_name:
    laptop_for_me = laptop1_price_key
    laptop1 = price_to_name[laptop_for_me]
    
for laptop_for_cat in price_to_name:
    laptop_for_cat = laptop2_price_key
    laptop2 = price_to_name[laptop_for_cat]
    
    
# print("laptop1 =", laptop1)
# print("laptop2 =", laptop2)


print("laptop1 =", price_to_name[laptop_for_me], "$")
print('laptop2 =', price_to_name[laptop_for_cat], "$")
print("\n")
print("To be more specyfic, you should take:")

print(price_to_name[laptop_for_me], "@", laptop_for_me, '$', "- for yourself.")
print("\n")
print("And for the cat (for watching birds on Youtube):", price_to_name[laptop_for_cat], "@", laptop_for_cat, "$", "- will be fine enough.")

print("\n")
total = 5000 - laptop_for_me - laptop_for_cat
how_many_percent_you_loose = (total * 100) / 5000

print("on this transaction you loose only:", total, '$')
print("it's only", how_many_percent_you_loose, "% from your voucher")

ps. this story is based on facts:

5 Likes

image
This code is producing this output, because you are asking to put a “$” after the name of the laptop.
image

My solution was for the problem:

from csv import reader
f =open(“laptops.csv”)
data = list(reader(f))
data = data[1:]
price_to_name = {}

for row in data:
price = int(row[2])
name = row[1]
#prices.append(row[2])
#names.append(row[1])
if price not in price_to_name:
price_to_name[price] = [name]
else:
price_to_name[price].append(name)
laptop1 = None
laptop2 = None
prices =
for price1 in price_to_name:
for price2 in price_to_name:
if price1+price2 == 5000 :
laptop1 = price_to_name[price1]
laptop2 = price_to_name[price2]
laptop1 = laptop1[0]
laptop2 = laptop2[1]
print(laptop1)
print(laptop2)
image

If you find it useful, please mark my answer as solution.

2 Likes

yes…indeed… It was middle of the night… now it’s obvious… Thank you:)

where I can mark it, as solution ?

3 Likes

@drill_n_bass , please check it out, I think you have to check for the solution option by clicking the three dot image below my previous reply, thank you.

1 Like

Hey folks,

There’s no way to mark a reply as solution here because this topic was created in the Social->Share category. We can only have solutions to topics created in the Q&A category (or one of its subcategories).

3 Likes

Would you please help to change the tag from social|share to Q|A ?

@ drill_n_bass Drill N Bass
I think you are able to mark my reply as solution now, please do it, thanks.

I did.
the box is matched. But it arise an " 500 error"

1 Like

@ drill_n_bass Drill N BassThanks, you’ve already done it.

Hi folks,
Here is my solution. Very similar to the one from last answer. Hopefully it is helpful to you.
Please give me advice if you have any better idea. Thanks.

Nice! I prefer doing it this way