简体   繁体   English

代码在 IDE 中有效,但在终端控制台中无效

[英]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).我的代码在 Pycharm 中运行良好,但是如果我在控制台(Ubuntu 终端)中键入 add 会出现错误。

The error I get in Console outside of Pycharm IDE:我在 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!我不确定我做错了什么......感谢任何帮助!

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.可能在 IDE 之外的 Python2 下运行它,其中input用于获取字符串并将其评估为 Python 表达式。 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.您输入的词似乎很可能是add (因为这是您将输入与之进行比较的内容之一),而 Python2 抱怨它无法评估它是理所当然的。

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. Python 2 raw_input等效于 Python 3 input因此您可以使用它,或者确保它由Python3而不是 Python2 运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码在 Jupiter notebook 中工作,但在终端中不能作为 .py 文件工作 - Code works in Jupiter notebook, but doesn't work in terminal as a .py file JavaScript 适用于 chrome 控制台,但不适用于 firefox 或 selenium 代码 - JavaScript works on chrome console but doesn't work in firefox or selenium code 代码中的错误是什么? 这似乎在我的 IDE 中运行良好,但在在线终端上不起作用 - What is the error in code? This seems to run fine in my IDE but doesn't work on online terminal cx_Oracle 在可执行文件中不起作用,但在 IDE 中起作用 - cx_Oracle doesn't work in executable but works in IDE 具有open(&#39;filename&#39;)的Python脚本可用于IDLE,但不能在控制台中使用 - Python script with open('filename') works with IDLE but doesn't work in console ZeroMQ 在 Unity 中不起作用,但可以作为控制台应用程序使用 - ZeroMQ doesn't work in Unity, but works as console application pycharm和子进程 - 在控制台中工作的东西在Pycharm中不起作用 - pycharm and subprocess - what works in console doesn't work in Pycharm 导入模块无法通过终端运行,而可以通过IDE运行 - Import module does not work through terminal, while it works through IDE 导入模块在 IDE 中有效,但在 windows 命令行中无效(已编辑) - Importing a module works in IDE but doesn't work in windows command line (edited) Python代码在函数外部起作用,但在函数内部不起作用 - Python code works outside function, but doesn't work inside function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM