1

I've search everywhere and I can't find the answer anywhere, I'va had trouble with my code, I'm trying to do a simple inventory with peewee. All I'm having is this error.

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

I've read that tabs mixed with spaces can make this happen, already checked that, even used the tool SublimeText has to replace tabs with spaces.

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

Any help or hint would be greatly appreciated. Thanks in advance.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
Uriel Coria
  • 92
  • 1
  • 8
  • 8
    Your indentation is completely messed up. – Paulo Bu Jan 13 '14 at 21:08
  • 3
    `for entry in options:` has two spaces before it; `if selection == '1':` has three. – senshin Jan 13 '14 at 21:08
  • 2 tabs on `for entry in options` and 6 on the next line?! Please fix the indentation on your code to something resembling regularity – inspectorG4dget Jan 13 '14 at 21:10
  • What is `class Meta`? Is it _supposed_ to be inside `Product`? – senshin Jan 13 '14 at 21:10
  • 1
    Run your code with `python -tt scriptname.py` and fix anything Python finds. – Martijn Pieters Jan 13 '14 at 21:10
  • 2
    @senshin: Yes, for ORM models (such as defined by Peewee and Django and WTForms) it is supposed to be there. – Martijn Pieters Jan 13 '14 at 21:10
  • I'm not sure why this question is being downvoted. The OP provides example code and lists what he has tried. +1 to counter. – Steven Rumbalski Jan 13 '14 at 21:21
  • I certainly am a noob, and my code is a bit messy I've been teaching myself Python this past week, and I'm having trouble with this identation thing since I've never used anything similar, and I don't know anyone near me who could guide me with this. I searched over and over for the problem and found out that it was a problem with tabs and spaces, I tried to correct it with Sublime Text but I kept having the same issue, so as a last resource I opened this question. – Uriel Coria Jan 13 '14 at 21:51

2 Answers2

5

Your indentation needs to be consistent. Pick an indentation size (4 spaces recommended) and stick with it. Each line should be indented at some multiple of this indentation size.

I've commented your code with the number of spaces preceding each line (note the inconsistencies):

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
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
1

I guess this is what you want:

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

I had to guess at some of the indentation because it was unclear to me which things were part of which blocks.

You need to get an editor that handles indentation properly. For best results, always indent using four spaces per level of indentation.

senshin
  • 10,022
  • 7
  • 46
  • 59
  • Thank you for the link, I will study that. Do you have any reccomendations on the editor for ubuntu? I guess sublime text didn't do his job well. – Uriel Coria Jan 13 '14 at 21:53
  • @UrielCoria Read through [this list](http://stackoverflow.com/questions/81584/what-ide-to-use-for-python) and see if anything catches your eye. – senshin Jan 13 '14 at 21:55