gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:19 25 4
gpt4 key购买 nike

我的代码在 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.')

我不确定我做错了什么...感谢任何帮助!

最佳答案

可能在 IDE 外的 Python2 下运行它,其中 input 用于获取字符串评估它就像它是一个 Python 表达式。您似乎正在输入单词 add(因为这是您将输入与之进行比较的对象之一),而 Python2 正提示它无法对其求值。

Python 2 raw_input 等同于 Python 3 input,因此您可以使用它,或者确保它由 Python3 运行比 Python2。

关于python - 代码在 IDE 中有效,但在终端控制台中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com