-1

I have on replit a discord.py file with an currency program. But it stuck at f: in the with open line

"

async def open_account(user):
  users = await get_bank_data()

  if str (user.id) in users:
      return False
  else:
        users[str(user.id)] = {}
        users[str(user.id)]["wallet"] = 0
        users[str(user.id)]["bank"] = 0
    with open("mainbank.json", "w") as f:
       json.dump(users, f)
       return True

At error it says: unindent does not match any outer indentation level

pls help me

Didier L
  • 18,905
  • 10
  • 61
  • 103

2 Answers2

2

Your indentation amounts need to be consistent, try sticking to only using TAB once.

Try replacing that section of code with:

async def open_account(user):
    users = await get_bank_data()

    if str (user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["wallet"] = 0
        users[str(user.id)]["bank"] = 0
    with open("mainbank.json", "w") as f:
        json.dump(users, f)
        return True

In the code you provided, you've started with an indentation of 2, then to 4, and then to 3. If you're using an IDE such as VSCode, there's a built-in option for configuring automatic indentation sizes. If youre on VSCode, try CTRL+P and type '>indent'

Trent112232
  • 169
  • 8
  • Hey! Thanks for your answer! No,i use Replit. But now it says this: async def open_account(user): users = await get_bank_data() if str (user.id) in users: return False else: users[str(user.id)] = {} users[str(user.id)]["wallet"] = 0 users[str(user.id)]["bank"] = 0 with open("mainbank.json", "w") as f: json.dump(users, f) return True async def get_bank_data(): with open("MainBank.json", "r") as f: users = json.load(f) return users Is wrong at with open("MainBank.json", "r") as f: – Vlad Andrei Jan 29 '22 at 14:13
  • 1
    Please clarify @VladAndrei, we can’t understand a single thing out of your comment – Łukasz Kwieciński Jan 29 '22 at 14:21
  • @VladAndrei Try remove 'async', as you shouldn't need this with anything that isn't a command or built-in discord.py function. For example, you won't need await or async for get_bank_data(). – Trent112232 Jan 29 '22 at 14:22
  • yes, but it is wrong at get_bank_data() with open("MainBank.json", "r") as f: users = json.load(f) return users it doesn't like the second line with open – Vlad Andrei Jan 29 '22 at 14:24
  • @VladAndrei Can you please provide us with the contents of get_bank_data – Trent112232 Jan 29 '22 at 15:55
0

Your indentation levels are messed up. Python uses whitespace to define blocks where most other languages would use braces. This works well as long as you use the same number of whitespaces for each indentation level all over your script.

So if you start with your first level (that has no indentation) and decide for a indentation amount of 4 whitespaces per level, your second level has 4 whitespaces, your third has 8, your fourth has 12, etc.

Wherever you have a second-level block, you have to give it 4 whitespaces of indentation, otherwise the Python interpreter will throw the error you got.

You'll find more information on how to do it right in the Indentation section of PEP8: https://www.python.org/dev/peps/pep-0008/#indentation

A good IDE can help you to format your code right ;-)

Fred
  • 1,103
  • 2
  • 14
  • 35