gpt4 book ai didi

python - 对Python中的AttributeError感到困惑

转载 作者:行者123 更新时间:2023-12-03 08:51:57 25 4
gpt4 key购买 nike

我正在尝试做以下项目:

想象一下,您已经开了一家小餐馆,并试图使其更容易接受和计算订单。由于您的餐厅仅出售9种不同的商品,因此您将每个商品分配给一个数字,如下所示。

Chicken Strips - $3.50
French Fries - $2.50
Hamburger - $4.00
Hotdog - $3.50
Large Drink - $1.75
Medium Drink - $1.50
Milk Shake - $2.25
Salad - $3.75
Small Drink - $1.25

为了快速接受订单,您的程序应允许用户键入数字字符串,然后应计算订单成本。例如,如果订购了一大杯饮料,两杯小饮料,两个汉堡包,一个热狗和一份沙拉,则用户应键入5993348,程序应说费用为$ 19.50。另外,请确保程序循环运行,以便用户可以接受多个订单,而不必每次都重新启动程序。

到目前为止,我的代码如下:
print "------------------Menu-------------------"

class Menu_Item():
def __init__(self, num,item,price):
self.num = num
self.item = item
self.price = price
def __repr__(self):
return "\n" + str(self.num) + ". " + self.item + " - $" + str(self.price) + ' dollars'



Strips = Menu_Item(1,'Chicken Strips', 3.50)
Fries = Menu_Item(2,"Fries",2.50)
Burger = Menu_Item(3,"Burger",4.00)
Hotdog = Menu_Item(4,"Hotdog",3.50)
Large_Drink = Menu_Item(5,"Large Drink",1.75)
Medium_Drink = Menu_Item(6,"Medium Drink", 1.50)
Milkshake = Menu_Item(7,"Milkshake", 2.25)
Salad = Menu_Item(8,"Salad", 3.75)
Small_Drink = Menu_Item(9,"Small Drink", 1.25)


Class_Items = [Strips,Fries,Burger,Hotdog,Large_Drink,Medium_Drink,Milkshake,Salad,Small_Drink]


print Class_Items

def take_order():
count = 0
string = raw_input("Enter your order")
order = []
for a in string:
order.append(a)
for food in Class_Items:
for b in order:
if b == Menu_Item.num:
count = count + Menu_Item.price
return count
else:
print "-"

take_order()

我收到以下错误。我已经尝试了很多方法来实现此目的,但我不知道为什么编译器无法识别num属性。关于为什么发生这种情况有什么建议吗?谢谢!
Traceback (most recent call last):
File "python", line 43, in <module>
File "python", line 37, in take_order
AttributeError: class Menu_Item has no attribute 'num'

最佳答案

您为什么在这里访问Menu_Item?那是类(class)本身;您从列表中获得的项目称为food

关于python - 对Python中的AttributeError感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38229726/

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