Jump to content

Error meaning and how to fix it?

Go to solution Solved by Avocado Diaboli,

You need to look at the indentation. That elif-statement isn't on the same depth as the if-statement, so the interpreter assumes you're trying to use an elif-statement without a preceding if-statement. Never mind that everything between the if- and elif-statements is probably not going to be executing the way you intend.

 

You have two options. Either indent everything between if and elif to the same depth, like this:

while True:
  ag=input("\nENTER E TO REROLL\n")
  if ag=="E" or ag=="e":
    
    for i in range(_q):
      r1=random.choice(list2)
      list1.append(r1)
      list2.remove(r1)
      
    print("LOADING...")
    time.sleep(2)
    os.system("clear")
      
    for i in list1:
      print(i, end="")
        
  elif ag=="!exit":
    break

Or you reorder the if and elif statements or turn the elif into a guard clause by basically asking if ag=="!exit" first and breaking immediately, instead of evaluating if it's "E" or "e" in the first place. Basically this:

while True:
  ag=input("\nENTER E TO REROLL\n")
  if ag=="!exit":
    break
  
  if ag=="E" or ag=="e":
    #do the rest
while True:
  ag=input("\nENTER E TO REROLL\n")
  if ag=="E" or ag=="e":

    for i in range(_q):
      r1=random.choice(list2)
      list1.append(r1)
      list2.remove(r1)


  print("LOADING...")
  time.sleep(2)
  os.system("clear")
  for i in list1:  
    print(i, end="")

elif ag=="!exit":
  break

If i run this code, it gives me an error. How do i fix it and what am i doing wrong? Edit: There is more code. This is only a fracture of it.

 File "main.py", line 37
    elif ag=="!exit":
    ^^^^
SyntaxError: invalid syntax
Edited by AmateurBuildr
Link to comment
Share on other sites

Link to post
Share on other sites

Does your application not have a debug mode where it can run the code and then halt and identify where the error is occurring ?

Last time I messed around with code was so long ago but simple commands and scripts etc the debug window would run in an isolated window and check for errors.

Link to comment
Share on other sites

Link to post
Share on other sites

You need to look at the indentation. That elif-statement isn't on the same depth as the if-statement, so the interpreter assumes you're trying to use an elif-statement without a preceding if-statement. Never mind that everything between the if- and elif-statements is probably not going to be executing the way you intend.

 

You have two options. Either indent everything between if and elif to the same depth, like this:

while True:
  ag=input("\nENTER E TO REROLL\n")
  if ag=="E" or ag=="e":
    
    for i in range(_q):
      r1=random.choice(list2)
      list1.append(r1)
      list2.remove(r1)
      
    print("LOADING...")
    time.sleep(2)
    os.system("clear")
      
    for i in list1:
      print(i, end="")
        
  elif ag=="!exit":
    break

Or you reorder the if and elif statements or turn the elif into a guard clause by basically asking if ag=="!exit" first and breaking immediately, instead of evaluating if it's "E" or "e" in the first place. Basically this:

while True:
  ag=input("\nENTER E TO REROLL\n")
  if ag=="!exit":
    break
  
  if ag=="E" or ag=="e":
    #do the rest

And now a word from our sponsor: 💩

-.-. --- --- .-.. --..-- / -.-- --- ..- / -.- -. --- .-- / -- --- .-. ... . / -.-. --- -.. .

ᑐᑌᑐᑢ

Spoiler

    ▄██████                                                      ▄██▀

  ▄█▀   ███                                                      ██

▄██     ███                                                      ██

███   ▄████  ▄█▀  ▀██▄    ▄████▄     ▄████▄     ▄████▄     ▄████▄██   ▄████▄

███████████ ███     ███ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀███▄ ▄██▀ ▀████ ▄██▀ ▀███▄

████▀   ███ ▀██▄   ▄██▀ ███    ███ ███        ███    ███ ███    ███ ███    ███

 ██▄    ███ ▄ ▀██▄██▀    ███▄ ▄██   ███▄ ▄██   ███▄ ▄███  ███▄ ▄███▄ ███▄ ▄██

  ▀█▄    ▀█ ██▄ ▀█▀     ▄ ▀████▀     ▀████▀     ▀████▀▀██▄ ▀████▀▀██▄ ▀████▀

       ▄█ ▄▄      ▄█▄  █▀            █▄                   ▄██  ▄▀

       ▀  ██      ███                ██                    ▄█

          ██      ███   ▄   ▄████▄   ██▄████▄     ▄████▄   ██   ▄

          ██      ███ ▄██ ▄██▀ ▀███▄ ███▀ ▀███▄ ▄██▀ ▀███▄ ██ ▄██

          ██     ███▀  ▄█ ███    ███ ███    ███ ███    ███ ██  ▄█

        █▄██  ▄▄██▀    ██  ███▄ ▄███▄ ███▄ ▄██   ███▄ ▄██  ██  ██

        ▀███████▀    ▄████▄ ▀████▀▀██▄ ▀████▀     ▀████▀ ▄█████████▄

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 6/1/2023 at 11:56 PM, Avocado Diaboli said:

Or you reorder the if and elif statements or turn the elif into a guard clause by basically asking if ag=="!exit" first and breaking immediately, instead of evaluating if it's "E" or "e" in the first place. Basically this:

From a readability standpoint I would recommend the reorder option.

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×