Reputation: 13
I am playing around with a simple blockchain. I wanna get the total number of transacted amounts in my code, but this Method only returns one value! which is the default value. Here is my sample chain.
"chain": [
{
"index": 1,
"previous_hash": "1",
"proof": 0,
"timestamp": "2021-07-13 12:46:27.100441",
"transactions": [
{
"amount": 0,
"reciver": "-",
"sender": "-"
}
]
},
{
"index": 2,
"previous_hash": "e267a3f77fe50af86466a9bc5fce5b8285cf3eff85260f402074505c6023a085",
"proof": 115558,
"timestamp": "2021-07-13 12:48:29.790718",
"transactions": [
{
"amount": 420.69,
"reciver": "John",
"sender": "Hoomehr"
},
{
"amount": 1142,
"reciver": "John",
"sender": "yaser"
},
{
"amount": 100,
"reciver": "Miner",
"sender": "01656f68288a45f993fc8c35c3d853d1"
}
]
},
{
"index": 3,
"previous_hash": "217a9bb5994943e03cef59edac125cc33c1831dce9d3ce8424ca134068e808b5",
"proof": 48245,
"timestamp": "2021-07-13 12:49:18.885861",
"transactions": [
{
"amount": 462,
"reciver": "Yaser",
"sender": "Hoomehr"
},
{
"amount": 32,
"reciver": "John",
"sender": "Hoomehr"
},
{
"amount": 100,
"reciver": "Miner",
"sender": "01656f68288a45f993fc8c35c3d853d1"
}
]
}
]
and this is my code:
I tried to go through each layer and do a for loop, meanwhile I do not know if my syntax is wrong or correct.
def total_trans(self):
balance=0
for i in self.chain:
for key , value in self.chain[0].items() :
for x in self.chain[0]['transactions']:
for k , v in self.chain[0]['transactions'][0].items():
balance += sum(self.chain[0]['transactions'][0]['amount'] for i in self.chain[0]['transactions'][0])
return balance
Upvotes: 0
Views: 157
Reputation: 39354
Yes, you keep referring to the first item in the chain instead of letting the for loops iterate over the data.
Try this:
def total_trans(self):
balance = 0.0
for i in self.chain:
for x in i['transactions']:
balance += x['amount']
return balance
With your data, I get balance
returned as 2256.69
In general, when you have a deeply nested data structure like this, you should write your program so it has just the outer most loop first: for i in self.chain:
in your case. Then you can just add print(i)
to see what you have. Then you would realise that you are after the transactions
and then you just keep going, iterating over something else, and printing, until you get what you want.
Upvotes: 1
Reputation: 1434
I made the original chain a python dictionary. If you are importing this JSON object from an external file you can use the JSON built in libray to deserialise it to a Python object.
block_chain = {"chain": [
{
"index": 1,
"previous_hash": "1",
"proof": 0,
"timestamp": "2021-07-13 12:46:27.100441",
"transactions": [
{
"amount": 0,
"reciver": "-",
"sender": "-"
}
]
},
{
"index": 2,
"previous_hash": "e267a3f77fe50af86466a9bc5fce5b8285cf3eff85260f402074505c6023a085",
"proof": 115558,
"timestamp": "2021-07-13 12:48:29.790718",
"transactions": [
{
"amount": 420.69,
"reciver": "John",
"sender": "Hoomehr"
},
{
"amount": 1142,
"reciver": "John",
"sender": "yaser"
},
{
"amount": 100,
"reciver": "Miner",
"sender": "01656f68288a45f993fc8c35c3d853d1"
}
]
},
{
"index": 3,
"previous_hash": "217a9bb5994943e03cef59edac125cc33c1831dce9d3ce8424ca134068e808b5",
"proof": 48245,
"timestamp": "2021-07-13 12:49:18.885861",
"transactions": [
{
"amount": 462,
"reciver": "Yaser",
"sender": "Hoomehr"
},
{
"amount": 32,
"reciver": "John",
"sender": "Hoomehr"
},
{
"amount": 100,
"reciver": "Miner",
"sender": "01656f68288a45f993fc8c35c3d853d1"
}
]
}
]
}
Then the nested loop extracts each "amount" and adds it to the balance variable, which is returned:
def total_trans(block_chain):
balance = 0
chain = block_chain["chain"] # get chain (list of blocks)
for block in chain: # each element in array (block, type dict)
for trans in block["transactions"]: # get transactions key
balance += trans["amount"] # add each amaout for each transaction to the balance
return balance
print(total_trans(block_chain)) # returns : 2256.69
Within your class it would look like:
def total_trans(self):
balance = 0
chain = self.block_chain["chain"] # get chain (list of blocks)
for block in chain: # each element in array (block, type dict)
for trans in block["transactions"]: # get transactions key
balance += trans["amount"] # add each amaout for each transaction to the balance
return balance
Upvotes: 2