2014-01-13 3 views
1

나는 어디에서나 검색을하고 어디에서나 대답을 찾을 수 없으며, 코드에 문제가 있습니다. 간단한 인벤토리를 사용하려고합니다. 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!" 

도움이나 힌트를 주시면 감사하겠습니다. 미리 감사드립니다.

+8

들여 쓰기가 완전히 엉망입니다. –

+3

'옵션에있는 입력 :'앞에는 공백이 두 개 있습니다. 'selection == '1':'은 세 개가 있습니다. – senshin

+0

'options in entry'에 2 탭, 다음 라인에 6 탭?! 코드의 들여 쓰기를 규칙 성을 닮은 것으로 고치십시오. – inspectorG4dget

답변

5

들여 쓰기가 일정해야합니다. 들여 쓰기 크기 (4 spaces recommended)를 골라서 붙이십시오. 각 줄은이 들여 쓰기 크기의 배수로 들여 쓰기되어야합니다.

내가 각 라인 (불일치주의) 앞 공간의 번호와 코드를 댓글을 달았습니다 :이 추측

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 
+0

감사합니다. 덕분에 많은 도움이되었습니다. –

1

당신이 원하는 :

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!" 

나는 생각했다 어떤 것들은 어느 블록의 일부인지 나에게 불분명하기 때문에 들여 쓰기의 일부에서.

들여 쓰기를 올바르게 처리하는 편집기를 가져야합니다. 최상의 결과를 얻으려면 들여 쓰기 수준 당 always indent using four spaces을 입력하십시오.

+0

링크를 가져 주셔서 감사합니다. 우분투 편집기에 대해 어떤 조언을 해주십니까? 나는 숭고한 텍스트가 그의 일을 잘하지 못했을 것으로 생각한다. –

+0

@UrielCoria [이 목록] (http://stackoverflow.com/questions/81584/what-ide-to-use-for-python)을 읽고 눈에 띄는 것이 있는지 확인하십시오. – senshin

관련 문제