[英]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.