简体   繁体   中英

How to let user create as many dictionaries as they want

I'm supposed to create an expense tracker that has the data fields "category, vendor, and amount." The user enters the category they want and it turns it into a dictionary. They then enter the vendors as keys and amounts as values. I was able to do that but the user is supposed to be able to create as many categories as they want. In my code, it just creates the one dictionary and when it loops back over it forgets the previous dictionary and makes a new one. Sorry for my phrasing for I am very new to python.

while True:
    user_category = input("What category do you want to enter? ")
    user_category = dict()
    user_vendor = int(input("How many vendors are you entering under this category? "))
    data_maker (user_category, user_vendor)

def data_maker (category, vendor_number):
    for i in range(vendor_number):
        vendor = input('Please enter a vendor name: ')
        transactions = [float(i) for i in input("Enter in each transaction for vendor entered 
        previously (seperated by spaces) (For example: 90 70.15 87.50): ").split(" ")]    
        category[vendor] = transactions
    print(category)

Below is the output

{'autoparts': [90.0, 91.5, 45.9]}

As you can see above, the user creates the category and makes it a dictionary. The user then enters how many vendors they will put under the category. This is then passed through the function "data_maker" to add the vendors and transactions to the dictionary.

Here is where my problem starts...

user_choices = input("If you would like to check total expense for a specific category type 'specific category'. If you would like to continue type 'continue'. If you would like to exit type 'exit'. ")
    if user_choices == "specific category":
        user_category_choice = input("what category do you want to check? ")
        specific_category_total (user_category_choice)
    if user_choices == "exit":
        break;

I want to know how I can create it so that the user can create as many categories as they please and then type the name of the category so that the code directly above this can get all the expenses for it. I would GREATLY appreciate it if someone would push me in the right direction. I don't know if I am unfortunate and just can't find the answer anywhere or if I am going about this in the wrong way.

There are some problems here. First, you get a value for user_category from the user, and then immediately throw it away, by assigning a new value to that variable. The next problem is that you do not have storage for all the category dictionaries.

Here is how you can modify the main loop:

all_dicts = {} # Make global storage for all dictionaries
while True:
    user_category = input("What category do you want to enter? ")
    # Make new category dictionary, if not already in global storage
    category_dict = all_dicts.setdefault(user_category, {})
    user_vendor = int(input("How many vendors are you entering under this category? "))
    # Use the dictionary you made/retrieved in data_maker()
    data_maker (category_dict, user_vendor)

The key here is to store all your category dictionaries in a dictionary (indexed by the category name). The setdefault dictionary method lets you use an object already associated with a key if one exists, or create a new object and association, and use the created object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM