【发布时间】:2015-03-20 12:14:56
【问题描述】:
定义一个名为 restaurant_price 的函数,该函数接受一个参数,即餐厅,并返回该餐厅价格字段的值。所以定义一个包含几家餐馆的列表
我不断收到餐厅未定义的错误。
这是我的代码:
def restaurant_price (Restaurant:Restaurant)-> float:
return Restaurant.price
from collections import namedtuple
Restaurant = namedtuple('Restaurant', 'name cuisine phone dish price')
RC = [
Restaurant("Thai Dishes", "Thai", "334-4433", "Mee Krob", 12.50),
Restaurant("Nobu", "Japanese", "335-4433", "Natto Temaki", 5.50),
Restaurant("Nonna", "Italian", "355-4433", "Stracotto", 25.50),
Restaurant("Jitlada", "Thai", "324-4433", "Paht Woon Sen", 15.50),
Restaurant("Nola", "New Orleans", "336-4433", "Jambalaya", 5.50),
Restaurant("Noma", "Modern Danish", "337-4433", "Birch Sap", 35.50),
Restaurant("Addis Ababa", "Ethiopian", "337-4453", "Yesiga Tibs", 10.50) ]
assert restaurant_price(RC[1]) == 5.50
那么我在第二个问题上需要帮助:编写一系列语句,按照从最便宜到最贵(最好的菜)的顺序打印出餐厅 RC 列表。
print(RC.sort(key=restaurant_price))
【问题讨论】:
-
欢迎来到 SO!请一次只问一个问题。请添加完整的错误消息及其在代码中的指示位置。
-
@howaboutNO 他的代码对我来说似乎语法不正确。
-
语法是正确的,请让他参考关于如何发布问题和错误的常见问题解答(但给他一个机会),是的,他的声明顺序不正确。
-
@howaboutNO 那是function annotation
-
顺便说一句:你可以这样使用
namedtuples 吗?我认为fields必须是可迭代的,所以它必须是['name','cuisine','phone','dish','price']
标签: python sorting namedtuple