cont = {} n = int(input("Insert the number of transactions:")) if n == 0: print("Going to the next option") else: for i in range(0, n): tranz = input("Insert the id of the transaction: ") ziua = input("Insert the day: ") suma = input("Insert the amount: ") tipul = input("Insert the type(int/out): ") tranz_key = tranz[0] cont[tranz] = {"day": ziua, "amount": suma, "type": tipul} k = input("Do you wish to update something?(Yes/No): ") if k == 'No': print("Going to the next option") else: y = input("Insert a new day:") if len(y) != 0: cont[tranz].update({"day": y}) z = input("Insert a new amount:") if len(z) != 0: z = int(z) cont[tranz].update({"amount": z}) w = input("Insert a new type(in/out):") if len(w) != 0: cont[tranz].update({"type": w})
Hello, I have to create a program that is able to add a transaction with the following details: day
, sum
, and type
. I don’t know how many transactions there will be so I found a code that I adapted to match my needs. In the last part of my code I want to try to update a specific dictionary (for example, if the user wants to modify the amount in a transaction with the id of 1, I should select that specific dictionary and modify it). I am a beginner in Python, but I have some knowledge regarding programming overall. Thanks in advance!
Advertisement
Answer
In your code, cont
is already a dictionary, you just have to replicate the same behaviour for the other dictionaries.
So for example, after the user input the program its transaction id (in tranz
) and the sum in z
, you could do something like this to edit only the sum:
cont[tranz][amount] = z
Be aware that this is going to work only if you already set cont[tranz]
as a dict
(you do it already in your range(0, n)
cycle)