-1

I'm confused and stucked when im trying to quit/ continue in if statement

def checkQuota():
   with open("Discord\Test Field\config.json", "r+") as file:
      data = json.load(file)
      quota = data['guild setting']['daily quota']
      if quota > 0:

I want to check if quota is not 0 then continue for execution, but when quota equals to 0, I want it to stop at there. Anyone can help me?

  • If you want to exit the function, just `return` anything (I believe you can also return nothing) – Larry the Llama Jan 13 '22 at 11:56
  • `continue` keyword only make sense inside loop, but there is not loop in your code snippet – Daweo Jan 13 '22 at 11:57
  • What is unclear? There is no loop here so there is nothing to quit or continue. If the condition is not true, the code will fall off the end of the function and return `None` – tripleee Jan 13 '22 at 11:57
  • 2
    Tangentially, you will want to use a raw string `r"Discord\Test Field\config.json"` or double the backslashes in the Windows path. See also https://stackoverflow.com/questions/2953834/windows-path-in-python – tripleee Jan 13 '22 at 11:59
  • @Daweo i'll try it –  Jan 13 '22 at 12:00

2 Answers2

2
def checkQuota():
    with open(r"Discord\Test Field\config.json", "r+") as file:
        data = json.load(file)

    quota = data['guild setting']['daily quota']
    if quota == 0:
        # out of quota
        return

    # ...rest of the function below
Tomalak
  • 332,285
  • 67
  • 532
  • 628
0
def checkQuota():
with open("Discord\Test Field\config.json", "r+") as file:
    data = json.load(file)
    item = data['guild setting']['daily quota']
    if item > 0:
        quotaChange = item - 1
        data['guild setting']['daily quota'] = quotaChange
        file.seek(0)
        json.dump(data, file, indent=4)
        file.truncate()
        
    else:
        print("Today's quota is 0, can't process command")

well i got it