Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord.py prints message 7 times instead of once

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@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)

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading