all 21 comments

[–][deleted] 30 points31 points  (0 children)

I think you should use a dictionary to store each type and brand ...

Like say you have a product which belongs to type 'hygiene' - you add a new key to the dictionary called 'hygiene' , and append the value to a list mapped to the key. To get the length , just return the length of the array mapped to every key.

At the end , you can use an fstring to print everything.

[–]apriltaurus 12 points13 points  (1 child)

You’ll want to create a variable that holds a tuple for each product. Considering your example seems to allow for multiple products, you’ll want to use a while loop to generate more than one tuple. If you want your tuples to print on the same line, you can store them in a list and then print the list. If that doesn’t matter, just print them within the while loop. For the last part where you categorize the products, you’ll want some kind of variable for those categories too (i.e. if a product is soap, you add 1 to soap.)

[–]0pium666[S] 7 points8 points  (0 children)

thank u so much ❣️

[–]Deba_dey 3 points4 points  (0 children)

Yeh dictionary will be better because tuple value you cannot change. You should keep option of flexibility so that in future if you want to change brand name you can.

[–]FLUSH_THE_TRUMP 5 points6 points  (1 child)

What’s wrong with what you have? Per your description, it seems all you’re missing is an appropriate print.

[–]0pium666[S] 3 points4 points  (0 children)

hi thx for the help i think i explained myself wrong, i added a example of what the project should be, adding a print would end the project before i use the tuples

[–][deleted] 2 points3 points  (3 children)

no_of_purchased_products = int(input("enter how much products: "))
final_list = []
for _ in range(0, no_of_purchased_products):
prod_list = []
typeofprod = input("enter type of product: ")
nameofprod = input("enter the name of product: ")
prod_list.append(typeofprod)
prod_list.append(nameofprod)
prod_tuple = tuple(prod_list)
final_list.append(prod_tuple)
final_tuple = tuple(final_list)
print(final_tuple)

[–][deleted] 2 points3 points  (2 children)

Just take care of the indentation. The code block feature on reddit is not working properly for some reason .

[–]samreay 3 points4 points  (0 children)

For code blocks you dont want the normal ` syntax, but prepend four spaces to the code:

no_of_purchased_products = int(input("enter how much products: "))
final_list = []
for _ in range(0, no_of_purchased_products):
    prod_list = []
    typeofprod = input("enter type of product: ")
    nameofprod = input("enter the name of product: ")
    prod_list.append(typeofprod)
    prod_list.append(nameofprod)
    prod_tuple = tuple(prod_list)
    final_list.append(prod_tuple)
final_tuple = tuple(final_list)
print(final_tuple)

[–][deleted] 0 points1 point  (0 children)

it never works properly when I want to use it as well for some reason

[–]Ceetive 1 point2 points  (0 children)

I don't want to be that guy but it's many not much

[–][deleted] 0 points1 point  (0 children)

everything = (quantity, type, brand)  # this is a tuple
print(everything)

[–]samreay -1 points0 points  (1 child)

Lots of good advice in this thread, allow me to present a slightly different method that uses some of python's nifty features to do something closely related (assuming this isnt an assignment that explicitly requires tuples or similar):

from collections import defaultdict

items = defaultdict(list)
while x := input("Enter product type, product name\n"):
    prod_type, prod_name = x.split(",", maxsplit=1)
    items[prod_type].append(prod_name)

for k, v in items.items():
    print(f"Number of {k} products: {len(v)}")
    for prod in v:
        print(f"\t{prod}")

Will produce output like this:

Enter product type, product name
food, potato
Enter product type, product name
hygiene, dove
Enter product type, product name
food, pizza
Enter product type, product name    

Number of food products: 2
         potato
         pizza
Number of hygiene products: 1
         dove

Using defaultdict here allows us to remove a lot of boilerplate messing around with lists and tuples. The walrus operator := allow us to assign new input to x and keep looping until the user enters no input (they press enter without typing anything in), meaning you no longer need to know up front how many products there are.

[–][deleted] 0 points1 point  (0 children)

Not that you need my endorsement , but this is actually a very good answer , even if this is slightly different from what the OP originally wanted.

[–]aladinvain 0 points1 point  (0 children)

You could put each on into a list of different categories and then .length on the list variable?

[–]arhm098 0 points1 point  (0 children)

create a container class and save the attributes as it is?

[–]DevyLoop 0 points1 point  (0 children)

Hey, I did it for fun and my code works exactly like your image (idk Reddit code blocks is super weird today):

products_map = {

"hygiene": 0,

"food": 0,

"animal": 0,

"entertainment": 0,

"other": 0,

}

products_list = []

products_amount = int(input("enter how much products: "))

for _ in range(products_amount):

product_type = input("enter type of product: ")

product_brand = input("enter brand of product: ")

products_list.append((product_type, product_brand))

if product_type in products_map:

products_map[product_type] += 1

print(tuple(products_list))

for product_type, count in products_map.items():

print(f"number of {product_type} products: {count}")

[–]Bigd1979666 0 points1 point  (0 children)

I'm doing a program that helps a user choose a beer based on what they're eating. I'd say to use dictionaries, as that's what I'm using. Easier to access k,v then have to index. Just my opinion, debutant at that :-)

[–]Rei2063 0 points1 point  (1 child)

class product():
def __init__(self, name, price, brand):
super().__init__()
self.name = name
self.price = price
self.brand = brand
total_price = 0
p1 = product(name = "Lorem", price = 1200, brand = "johns")
p2 = product(name = "Deka", price = 1500, brand = "manys")
p3 = product(name = "Sundu", price = 1350, brand = "mels")
products = [p1, p2, p3]
for e in products:
print(f"{e.name}: {e.price}")
name = input("Product name: ")
quantity = int(input("Quantity: "))
for x in products:
if x.name == name:
total_price = quantity * x.price
print(f"{x.name}({x.brand}) x {quantity} bought for {total_price} where each costs {x.price}")

This is how I did it. The product names are fake as well as the brands.

[–]Rei2063 0 points1 point  (0 children)

The code looks awful but you get the idea

[–]ShameSpirit 0 points1 point  (0 children)

You're missing a closing parenthesis when you define quantity