如何处理缩进错误:取消缩进与任何外部缩进级别都不匹配



我到处搜索,在任何地方都找不到答案,我的代码遇到了问题,我正在尝试用peewee做一个简单的清单。我遇到的只是这个错误。

  File "inventario.py", line 38
if selection =='1':
                  ^
IndentationError: unindent does not match any outer indentation level

我读过与空格混合的制表符可以实现这一目标,已经检查过,甚至使用了SublimeText必须用空格替换制表符的工具。

from peewee import *
db = SqliteDatabase('stockpy.db') #creates db
class Product(Model): #data to create product table
    id_prod = IntegerField()
    name = CharField()
    price = IntegerField()
    stock = IntegerField()
    class Meta:
        database = db #tells which db to use 
Product.create_table() #creates table
def newProd(id_prod, name, price, stock):
     Product.create(id_prod =  id_prod, name = name, price = price, stock = stock) #adds new product with input
def deleteProd(name): #Deletes de product with the given name
     todelete = Product.get(Product.name == name)
     todelete.delete_instance()
def viewStock():
     for name in Product.select(): #shows whats in the table
      print Product.id_prod, Product.name, Product.price, Product.stock
menu = {} #simple menu
menu['1']="Add product." 
menu['2']="Delete product."
menu['3']="View stock"
menu['4']="Exit"
while True: 
  options=menu.keys()
  options.sort()
  for entry in options: 
        print entry, menu[entry]
        selection=raw_input("Please Select:")
   if selection =='1':
        print "Need the following data:"
        id_prod = raw_input("Product id: ")
        int(id_prod)
        name = raw_input("Product name: ")
        price = raw_input("Product price: ")
        int(price)
        stock = raw_input ("How many are there: ")
        int(stock)
        print "You're adding the following data", id_prod, name, price, stock
      newProd()
   elif selection == '2':
        name = raw_input ("Enter the name of the product you want to delete: ")
       deleteProd(name)
   elif selection == '3':
        print "Here's your stock"
        viewStock()
   elif selection == '4':
         print "Goodbye"
         break
   else:
         print "Unknown Option Selected!"

任何帮助或提示将不胜感激。提前谢谢。

缩进需要保持一致。 选择一个缩进大小(建议使用 4 个空格)并坚持使用。 每行应在此缩进大小的某个倍数缩进。

我已经用每行前面的空格数注释了您的代码(请注意不一致之处):

menu = {} #simple menu                             # 0
menu['1']="Add product."                           # 0
menu['2']="Delete product."                        # 0
menu['3']="View stock"                             # 0
menu['4']="Exit"                                   # 0
while True:                                        # 0
  options=menu.keys()                              # 2
  options.sort()                                   # 2
  for entry in options:                            # 2
        print entry, menu[entry]                   # 8
        selection=raw_input("Please Select:")      # 8
   if selection =='1':                             # 3
        print "Need the following data:"           # 8
        id_prod = raw_input("Product id: ")        # 8
        int(id_prod)                               # 8
        name = raw_input("Product name: ")         # 8
        price = raw_input("Product price: ")       # 8
        int(price)                                 # 8
        stock = raw_input ("How many are there: ") # 8
        int(stock)                                 # 8
        print "You're adding the following data", id_prod, name, price, stock # 8
      newProd()                                    # 6
   elif selection == '2':                          # 3
        name = raw_input ("Enter the name of the product you want to delete: ") # 8
       deleteProd(name)                            # 7
   elif selection == '3':                          # 3
        print "Here's your stock"                  # 8
        viewStock()                                # 8
   elif selection == '4':                          # 3
         print "Goodbye"                           # 9
         break                                     # 9
   else:                                           # 3
         print "Unknown Option Selected!"          # 9

我想这就是你想要的:

from peewee import *
db = SqliteDatabase('stockpy.db') #creates db
class Product(Model): #data to create product table
    id_prod = IntegerField()
    name = CharField()
    price = IntegerField()
    stock = IntegerField()
    class Meta:
        database = db #tells which db to use 
Product.create_table() #creates table
def newProd(id_prod, name, price, stock):
     Product.create(id_prod=id_prod,
                    name=name,
                    price=price,
                    stock=stock) #adds new product with input
def deleteProd(name): #Deletes de product with the given name
     todelete = Product.get(Product.name == name)
     todelete.delete_instance()
def viewStock():
    for name in Product.select(): #shows whats in the table
        print Product.id_prod, Product.name, Product.price, Product.stock
menu = {} #simple menu
menu['1'] = "Add product." 
menu['2'] = "Delete product."
menu['3'] = "View stock"
menu['4'] = "Exit"
while True: 
    options = menu.keys()
    options.sort()
    for entry in options: 
        print entry, menu[entry]
        selection=raw_input("Please Select:")
    if selection =='1':
        print "Need the following data:"
        id_prod = raw_input("Product id: ")
        int(id_prod)
        name = raw_input("Product name: ")
        price = raw_input("Product price: ")
        int(price)
        stock = raw_input ("How many are there: ")
        int(stock)
        print "You're adding the following data", id_prod, name, price, stock
        newProd()
    elif selection == '2':
        name = raw_input ("Enter the name of the product you want to delete: ")
        deleteProd(name)
    elif selection == '3':
        print "Here's your stock"
        viewStock()
    elif selection == '4':
        print "Goodbye"
        break
    else:
        print "Unknown Option Selected!"

我不得不猜测一些缩进,因为我不清楚哪些东西是哪些块的一部分。

您需要一个正确处理缩进的编辑器。为获得最佳效果,请始终缩进每个缩进级别使用四个空格。

相关内容

最新更新