Reputation: 33
I have a code that matches the user's coins to a pattern and then tells the user the number of matches that they have. But for some reason at the end when I display the number of matches it displays it 7 times.
The code is:
@client.command()
async def matches(ctx):
with open("Shibaku1.json") as f:
coins_data = json.load(f)
matches = 0
for coin1 in coins_data[str(ctx.author.id)]:
if coin1 == dcoin1:
matches = matches + 1
else:
matches = matches
for coin2 in coins_data[str(ctx.author.id)]:
if coin2 == dcoin2:
matches = matches + 1
else:
matches = matches
for coin3 in coins_data[str(ctx.author.id)]:
if coin3 == dcoin3:
matches = matches + 1
else:
matches = matches
for coin4 in coins_data[str(ctx.author.id)]:
if coin4 == dcoin4:
matches = matches + 1
else:
matches = matches
for coin5 in coins_data[str(ctx.author.id)]:
if coin5 == dcoin5:
matches = matches + 1
else:
matches = matches
for coin6 in coins_data[str(ctx.author.id)]:
if coin6 == dcoin6:
matches = matches + 1
else:
matches = matches
if matches > 0:
await ctx.send(f'Shibaku1 has {matches} matches')
else:
matches = 0
The code to store user coins in "Shibaku1.json"
is:
@client.command()
async def Shibaku1(ctx, coin1, coin2, coin3, coin4, coin5, coin6, shibakunumber):
with open('Shibaku1.json', 'r') as f:
coins_data = json.load(f)
coins_data[str(ctx.author.id)] = (coin1, coin2, coin3, coin4, coin5, coin6, shibakunumber)
with open('Shibaku1.json', 'w') as f:
json.dump(coins_data, f)
Upvotes: 1
Views: 65
Reputation: 529
Change those last lines:
for coin6 in coins_data[str(ctx.author.id)]:
if coin6 == dcoin6:
matches = matches + 1
else:
matches = matches
if matches > 0:
await ctx.send(f'Shibaku1 has {matches} matches')
else:
matches = 0
in:
for coin6 in coins_data[str(ctx.author.id)]:
if coin6 == dcoin6:
matches = matches + 1
else:
matches = matches
if matches > 0:
await ctx.send(f'Shibaku1 has {matches} matches')
else:
matches = 0
Your indentation is wrong, so the if statement will loop in the for-loop.
Upvotes: 1
Reputation: 7963
Much more understandable and conscious (but note that dcoin1, ..., dcoin6
are not defined):
@client.command()
async def matches(ctx):
with open("Shibaku1.json") as f:
coins_data = json.load(f)
matches = 0
for coin in coins_data[str(ctx.author.id)]:
for ref in [dcoin1, dcoin2, dcoin3, dcoin4, dcoin5, dcoin6]:
if coin == ref:
matches += 1
if matches > 0:
await ctx.send(f'Shibaku1 has {matches} matches')
Or (better or worse - it depends) use the fact that bool
is interpreted as integer in addition context.
@client.command()
async def matches(ctx):
with open("Shibaku1.json") as f:
coins_data = json.load(f)
matches = sum(coin == ref
for coin in coins_data[str(ctx.author.id)]
for ref in [dcoin1, dcoin2, dcoin3, dcoin4, dcoin5, dcoin6])
if matches > 0:
await ctx.send(f'Shibaku1 has {matches} matches')
Also note that, for instance, if coins
and dcoins
are both unique sets, then you can write code even more readable using set
Upvotes: 1
Reputation: 2581
The problem is with identation. Do this:
@client.command()
async def matches(ctx):
with open("Shibaku1.json") as f:
coins_data = json.load(f)
matches = 0
for coin1 in coins_data[str(ctx.author.id)]:
if coin1 == dcoin1:
matches = matches + 1
else:
matches = matches
for coin2 in coins_data[str(ctx.author.id)]:
if coin2 == dcoin2:
matches = matches + 1
else:
matches = matches
for coin3 in coins_data[str(ctx.author.id)]:
if coin3 == dcoin3:
matches = matches + 1
else:
matches = matches
for coin4 in coins_data[str(ctx.author.id)]:
if coin4 == dcoin4:
matches = matches + 1
else:
matches = matches
for coin5 in coins_data[str(ctx.author.id)]:
if coin5 == dcoin5:
matches = matches + 1
else:
matches = matches
for coin6 in coins_data[str(ctx.author.id)]:
if coin6 == dcoin6:
matches = matches + 1
else:
matches = matches
if matches > 0:
await ctx.send(f'Shibaku1 has {matches} matches')
else:
matches = 0
Upvotes: 1