代码在IDE中工作,但在终端中不工作

2024-09-26 22:51:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码在Pycharm中运行得非常好,但是如果我在控制台(Ubuntu终端)中键入add,就会出错。你知道吗

我在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

我的代码:

#!/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.')

我不确定我做错了什么。。。感谢您的帮助!你知道吗


Tags: inselfaddinputitemquantityintprint
1条回答
网友
1楼 · 发布于 2024-09-26 22:51:05

您可能在IDE之外的Python2下运行它,其中input用于获取字符串并将其作为Python表达式进行计算。似乎您正在输入单词add(因为这是您比较输入的原因之一),Python2有理由抱怨它无法计算它。你知道吗

Python2raw_input与Python3input等价,因此您可以使用它,或者确保它由Python3而不是Python2运行。你知道吗

相关问题 更多 >

    热门问题