Reputation: 1557
I'm making a money bot and I want to count up all values together.
My code now:
@bot.command()
async def stats(ctx):
with open('casinomoney.json', 'r') as f:
users = json.load(f)
for k, v in users.items():
statscash = int(users[k]['Cash'])
statsbank = int(users[k]['Bank'])
statstotal = int(users[k]['Total'])
break
print(f'Cash: {statscash}')
print(f'Bank: {statsbank}')
print(f'Total: {statstotal}')
My json file:
{
"305354423801217025": {
"Bank": 1001,
"Cash": 0,
"Total": 1001
},
"369910055665008652": {
"Bank": 1001,
"Cash": 0,
"Total": 1001
}
}
For Bank
for example, it should be 2002 at the end.
Upvotes: 0
Views: 304
Reputation: 6234
omit the break
statement and also when you're iterating over your users
dictionary you should avoid accessing key by users[k]['Cash']
instead you can directly use v['Cash']
.
@bot.command()
async def stats(ctx):
with open("casinomoney.json", "r") as f:
users = json.load(f)
statscash = 0
statsbank = 0
statstotal = 0
for k, v in users.items():
statscash += int(v["Cash"])
statsbank += int(v["Bank"])
statstotal += int(v["Total"])
print(f"Cash: {statscash}")
print(f"Bank: {statsbank}")
print(f"Total: {statstotal}")
Cash: 0
Bank: 2002
Total: 2002
Upvotes: 1