简体   繁体   中英

Why does it give me unexpected unindent=

Why does the function crear_bd gives me unexpected indent? I think that is in a good position or its something else that could be affecting this? I defined the crear_bd function, I just didn't include it here. I'm a beginner.

def agregar_categoria():
    categoria = input("¿Como quieres nombrar a la nueva categoria?\n> ")

    conexion = sqlite3.connect("restaurante.db")
    cursor = conexion.cursor()

    try:
        cursor.execute(''' INSERT INTO categoria (null, '{}')'''.format(categoria))

        conexion.commit()
        conexion.close()
    
crear_bd()

while True:
    print("\nBienvenido al gestor del restaurante!")
    opcion = input("\nIntroduce una opción:\n[1] Agregar una categoría\n ")
        
    

I think you cannot use a try without an except or finally . So just add an except: block beneath your try block and it should work fine. You can simply do an

except:
   pass

to ignore all exceptions, but I would not recommend this. It is better to think of an useful exception handling.

This is no problem of mixing spaces and tabs, although this is often the source of unexpected unindent . But in this case, python expects the except block and this block needs to be indented. So the missing except is hidden behind the unindent error.

Try this, the try block was not properly indented

  categoria = input("¿Como quieres nombrar a la nueva categoria?\n> ")

  conexion = sqlite3.connect("restaurante.db")
  cursor = conexion.cursor()
  try:
      cursor.execute(''' INSERT INTO categoria (null, '{}')'''.format(categoria))
      conexion.commit()
      conexion.close()
  except:
      pass      
      
    
crear_bd()

while True:
       print("\nBienvenido al gestor del restaurante!")
       opcion = input("\nIntroduce una opción:\n[1] Agregar una categoría\n ")

For Sublime Text users:

Set your editor to use tabs for indentation: View --> Indentation --> Convert Indentation to Tabs

Also, uncheck the Indent Using Spaces option as well in the same sub-menu above.

This error happens when you use space and tabs for indentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM