Search code examples
pythonindentation

I got a problem on a currency project on discord.py


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


Solution

  • 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 ;-)