How to have a new list for every input

I am trying to edit the input entered for each day. I have created an input_sales_day function that contains a number of products to enter for a day, an input_sales function that takes the number of products and days as parameters, where I think the problem lies, and a final function that just prints. I’ve tried using split, but I always get the error or just print each word instead.

Here is the code, it prints:

Product name: z1
quantity sold : 1
Product Name: z1
quantity sold : 1
Product name : z2
quantity sold : 2
Product Name: z2
quantity sold : 2
Product name : z3
quantity sold : 3
Product Name: z3
quantity sold: 3
Day 1 : ['1 z1', '1 z1']
Day 2 : ['1 z1', '1 z1', '2 z2', '2 z2']
Day 3: ['1 z1', '1 z1', '2 z2', '2 z2', '3 z3', '3 z3']

I try to print:

Day 1: ['1 z1', '1 z1']
Day 2 : ['2 z2', '2 z2']
Day 3 : ['3 z3', '3 z3']
p = []
def input_sales_day(nbp):
    for i in range(nbp):
        np = input("Product Name: ")
        qv = input("quantity sold : ")
        p.append('{} {}'.format(qv, np))
    return p

def input_sales(nbp, d):
    sl = []
    for j in range(d):
        n = input_sales_day(nbp)
        sl.append('day {} : {}'.format(j+1, n))
    return sl

def print_sales(sl):
    return '\n'.join(sl)

print(print_sales(input_sales(2, 3)))

>Solution :

All you need to do is make p a local variable of the input_sales_day function. If you do this, then p will be reset on every invokation. Like this:

def input_sales_day(nbp):
    p = []
    for i in range(nbp):
        np = input("Product Name: ")
        qv = input("quantity sold : ")
        p.append('{} {}'.format(qv, np))
    return p


def input_sales(nbp, d):
    sl = []
    for j in range(d):
        n = input_sales_day(nbp)
        sl.append('day {} : {}'.format(j+1, n))
    return sl


def print_sales(sl):
    return '\n'.join(sl)


print(print_sales(input_sales(2, 3)))

Leave a Reply