所以给我一个菜单和食物评级,我必须创建一个数据结构,然后创建一个getInfo函数,并找到最便宜的函数。
getInfo(item)需要1个项目并打印出其价格和评级 findCheapest(item1,item2)接收2个项目,检查项目是否在菜单中并且给出最便宜的。我在这里写的很远,我正在努力奋斗。
如何使所有输入大小写对两个函数都不敏感,但仍然返回正确格式化的单词。例如:
getInfo(" tEa")应该返回: 茶价:7。评级:4
我知道你可以进行字符串比较并将其转换为小写,但你不能在设定的原因中这样做,那么它会打印错误,你怎么做,仍然正确地比较每个值。
我正在努力的第二部分是findCheapest函数如何使它可以采取任意数量的值,并且仍然打印最便宜而不会过多地改变数据结构。
所以我实现了一个formatText(item)函数,可以转换为正确的格式。
以下是代码:
menu= {"Tea", "Coffee", "Cookie", "Chips"}
price={
"Tea": 7,
"Coffee": 5,
"Cookie": 2,
"Chips": 3
}
rating= {
"Tea": 4,
"Coffee": 4.5,
"Cookie":5,
"Chips": 2
}
def getInfo(item):
if item in menu:
print item + "- price: %s." % price[item] + " Ratings %s" %rating[item]
else:
print "This item was not found: " + item
def findCheapest (item1,item2):
if item1 in menue and item2 in menue:
if (price[item1] < price[item2]):
print item2+ " is the cheapest"
elif (price[item1] > price[item2]):
print item1 +" is the cheapest"
else:
print "An item was not found."
getInfo("tEa")
getInfo("coFfeE")
findCheapest("tEa", "coFfeE")
答案 0 :(得分:0)
无论用户输入如何,都使用大写功能。并获得价格和评级。只用大写中每个单词的第一个字母来衡量回报,其他所有单词都降低
list_of_items = [x.capitalize() for x in raw_input.split()]
find_cheapest(list_of_items)
def find_cheapest(list_of_items):
cheapest_price = 12345
cheapest_item = ""
for item in list_of_items:
# Get the price of the item and check with the least price
item_price = price.get(item,None)
if item_price and item_price < cheapest_price:
cheapest_price = item_price
cheapest_item = item
if cheapest_item:
return cheapest_item + "is the cheapest"
else:
return "No Items were found"
答案 1 :(得分:0)
使用@Aswin Murugesh解决方案将所有内容都设为大写或小写字母
或
根据您当前的设置,您可以使用capitalize()
或
构造跟随字符串(使用upper()
使用lower()
+ rest char lower使用getInfo("tEa".capitalize())
getInfo("coFfeE".capitalize())
findCheapest("tEa".capitalize(), "coFfeE".capitalize())
)来创建第一个char资本。
SELECT
(SELECT firstname FROM registration_details a INNER JOIN users b
ON a.id = b.registerid
WHERE c.patientid = b.id ) AS patient,
(SELECT firstname FROM registration_details a INNER JOIN users b
ON a.id = b.registerid
WHERE c.doctorid = b.id ) AS doctor,
c.`appoinmentdate`
FROM `appoinments` c
将其应用于输入值或作为方法的第1步
答案 2 :(得分:0)
my_menu= ["Tea", "Coffee", "Cookie", "Chips"] price={ "Tea": 7, "Coffee": 5, "Cookie": 2, "Chips": 3 } rating= { "Tea": 4, "Coffee": 4.5, "Cookie":5, "Chips": 2 } def isItemInMenue(item="", menu=[]): for x in menu: if x.lower() == item.lower(): return x return "" def getInfo(item): item_ = isItemInMenue(item, my_menu) if item_: print item_ + " - price: %s." % price[item_], "Ratings %s" % rating[item_] else: print "This item was not found: " + item_ def findCheapest (item1, item2): item1_ = isItemInMenue(item1, my_menu) item2_ = isItemInMenue(item2, my_menu) if item1_ and item2_: if (price[item1_] price[item2_]): print item1_ + " is the cheapest" else: print "An item was not found." getInfo("tEa") getInfo("coFfeE") findCheapest("tEa", "coFfeE")
答案 3 :(得分:-1)
upper
和lower
函数在执行逻辑之前正确格式化项目。
def format_item(someItem):
return " ".join(x[0].upper() + x[1:].lower() for x in someItem.split())
print format_item("tEA") #Tea
然后你可以在逻辑的其余部分之前调用格式项。例如。
def findCheapest (item1,item2):
item1 = format_item(item1)
item2 = format_item(item2)
...