lundi 28 septembre 2020

Applying function to which element in a list?

So i wrote this code:

def bool_gen(p):
   p = float(p)
   if p > 100 or p < 0:
     p = 0.5
   elif 1 <= p <= 100:
     p = p / 100

   return random.random() < p

def apply_discount(v, b):

    if b == True:
       v = v * 0.5
       return v
    elif b == False:
       return v


p = int(random.randint(0,200))
b = bool_gen(p)       
purchases_prices = [20,30,40,50]
have_discount = []
no_discount = []
for each_price in purchases_prices: 
   if b == True    
      have_discount.append(apply_discount(each_price,b))
        
   elif b == False:   
       no_discount.append(apply_discount(each_price,b))

What i want is to bool_gen is applied to each element in purchases_prices and not to the whole list. This is what i get:

have_discount = [10, 15, 20] and no_discount = []

What i wanted to happen(so apply_discount to be applied in each of the element in purchases_prices):

have_discount = [10,20]  and no_discount = [30]

Ty in advance!




Aucun commentaire:

Enregistrer un commentaire