Home > Back-end >  How do access entire value inside list inside dictionary, instead of single character?
How do access entire value inside list inside dictionary, instead of single character?

Time:03-08

I'm brand new to coding and I have tried looking things up and rereading my notes but I can't figure this one out. I'm trying to retrieve the individual values inside the lists inside the dictionary (all_customers). Example of when I'm trying to retrieve a number:

print(f"Earnings from how many months they subscribed for = ${(all_customers['customer1'][0])}")

But while indexing, it retrieves individual characters (like in the example above it returns the bracket: [), instead of a full number (like 151). It should be more like: if I input 25 for months_subscribed, 10 for ad_free_months, and 5 for videos_on_demand_purchases for the first customer, all_customers['customer1'] should return [151, 20, 139.95] and the example that I tried to print up above should read "Earnings from how many months they subscribed for = $151" instead of "Earnings from how many months they subscribed for = [".

def subscription_summary(months_subscribed, ad_free_months, video_on_demand_purchases):  

    #price based on months subscribed
    if int(months_subscribed) % 3 == 0:
        months_subscribed_price = int(months_subscribed)/3*18
    elif int(months_subscribed) > 3:
        months_subscribed_price = int(months_subscribed)%3*7   int(months_subscribed)//3*18
    else:
        months_subscribed_price = int(months_subscribed)*7
        
    #price of ad free months
    ad_free_price = int(ad_free_months)*2

    #price of on demand purchases
    video_on_demand_purchases_price = int(video_on_demand_purchases)*27.99

    customer_earnings = [months_subscribed_price, ad_free_price, video_on_demand_purchases_price]

    return customer_earnings
#Loop through subscription summary 3 times, to return 3 lists of customers earnings and add them to a dictionary
all_customers={}

for i in range(3):
    months_subscribed = input("How many months would you like to purchase?: ")
    ad_free_months = input("How many ad-free months would you like to purchase?: ")
    video_on_demand_purchases = input("How many videos on Demand would you like to purchase?: ")

    #congregate individual customer info into list and run it through the function
    customer = [months_subscribed, ad_free_months, video_on_demand_purchases]

    indi_sub_sum = subscription_summary(customer[0], customer[1], customer[2])
    #congregate individual customers subscription summary lists into a dictionary
  
    all_customers[f"customer{i 1}"] = f"{indi_sub_sum}"

I hope this is an okay question to ask! Sorry, I'm new to this stuff:)

CodePudding user response:

Try printing out the all_customers dictionary and you will see what the problem is:

>>> all_customers
{'customer1': '[32, 8, 83.97]', 'customer2': '[25, 6, 55.98]', 'customer3': '[18.0, 4, 27.99]'}

All your lists are actually strings. That's because of this line:

all_customers[f"customer{i 1}"] = f"{indi_sub_sum}"

You are assigning a string to it instead of the list. Changing it to:

all_customers[f"customer{i 1}"] = indi_sub_sum

should solve it for you.

  • Related