Daniel C
Daniel C

Reputation: 13

Code works in IDE but doesn't work in Terminal Console

My code works perfectly in Pycharm but I get an error if I type add in the Console (Ubuntu Terminal).

The error I get in Console outside of Pycharm IDE:

Traceback (most recent call last):
  File "main.py", line 37, in <module>
    getStr = input('>: ')
  File "<string>", line 1, in <module>
NameError: name 'add' is not defined

My Code:

#!/user/bin/python3
class Item:

    itemsCount = 0

    def __init__(self, sku, bWidth, bHeight, bLength, quantity, bWeight):

        self.sku = sku
        self.bWidth = bWidth
        self.bHeight = bHeight
        self.bLength = bLength
        self.quantity = quantity
        self.bWeight = bWeight
        Item.itemsCount += 1

    def DisplayItem(self):
        print('[SKU : ', self.sku, '] [Width : ', self.bWidth, '] [Height : ', self.bHeight,
              '] [bLength : ', self.bLength, '] [Quantity : ', self.quantity, '] [bWeight : ',
              self.bWeight, ']')


items = [Item]


print('Dan\'s Warehouse Inventory')
print('Current Stock in inventory : [', Item.itemsCount,']\n' )


while True:

    getStr = input('>: ')

    if getStr == 'add':
        getSku = input('SKU : ')
        getWidth = int(input('Width : '))
        getHeight = int(input('Height : '))
        getLength = int(input('bLength : '))
        getQuantity = int(input('Quantity : '))
        getWeight = int(input('Weight : '))

        items.append(Item(getSku, getWidth, getHeight, getLength, getQuantity, getWeight))
        print(Item.itemsCount)

    else:
        print('Invalid command.')

I'm not sure what I am doing wrong... Any help is appreciated!

Upvotes: 1

Views: 395

Answers (1)

paxdiablo
paxdiablo

Reputation: 882206

You're probably running it under Python2 outside the IDE, where input is used to get a string and evaluate it as if it were a Python expression. It seems likely that you're entering the word add (since that's one of the things you compare the input against) and Python2 is rightfully complaining that it cannot evaluate it.

The Python 2 raw_input is equivalent to the Python 3 input so you could either use that, or ensure that it's run by Python3 rather than Python2.

Upvotes: 1

Related Questions