添加具有货币格式的变量和搜索列表的最佳解决方案
Adding variables with currency formatting and the best solution to search a list
我有以下代码:
完整代码清单:https://repl.it/JSUN/1
出于教学目的,我希望获得最好的 method/solution 两件事:
Q1. 使用 货币变量 ,例如存储在下面篮子列表中的变量
basket=[['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]
在checkout sub中,目的是将相应商品的COSTS加在一起。
因此,如果购物篮包含上述物品,则所需的输出 将是 £900+£1020+£700
=£2620
我已经尝试过各种方法,例如转换为整数,这显然行不通,我认为某种字符串操作可能是唯一的出路(这似乎不必要地复杂)。像 VB.Net 这样的语言有一个货币数据类型,这将使这个任务变得相当简单。解决这个问题的 pythonic 方法是什么?
Q2. 循环遍历整个购物篮以在结账时生成购物篮中的所有商品,而不仅仅是前 2
我试过了,没用:
for items in basket:
print("Your total bill is:", basket[items][1])
items=items+1
还有这个,也是错误的:
for i in len(basket):
print("Your total bill is:", basket[i][1])
错误:
TypeError: 'int' object is not iterable
这两个问题相互关联,因为它们都与 checkout() 子项有关,其中 addition of all 货币变量在篮子名单中的是objective!
Q1:
考虑到它仅用于基本教学目的,您可以去掉井号(假设表示一致):
def extract_cost(money_in_pounds):
return int(money_in_pounds[1:])
basket = [['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]
total_cost = 0
for items in basket:
total_cost = total_cost + extract_cost(items[1])
print(total_cost)
不写另一个函数
basket = [['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]
total_cost = 0
for items in basket:
# items[1] selects the cost
# items[1][1:] selects the sub-string of that cost from the 1st index to the end, i.e. remove the currency notation
# int() then converts it into an integer
cost_in_pounds = int(items[1][1:])
total_cost = total_cost + cost_in_pounds
print(total_cost)
更简洁的代码(以下由 Jon Clements 建议)
total_cost = sum(int(cost[1:]) for item_name, cost in basket)
Q2:
items
不是索引,是列表的内容,所以items本身就是内列表:
for items in basket:
print("Your total bill is:", items[1])
我有以下代码:
完整代码清单:https://repl.it/JSUN/1
出于教学目的,我希望获得最好的 method/solution 两件事:
Q1. 使用 货币变量 ,例如存储在下面篮子列表中的变量
basket=[['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]
在checkout sub中,目的是将相应商品的COSTS加在一起。
因此,如果购物篮包含上述物品,则所需的输出 将是 £900+£1020+£700 =£2620
我已经尝试过各种方法,例如转换为整数,这显然行不通,我认为某种字符串操作可能是唯一的出路(这似乎不必要地复杂)。像 VB.Net 这样的语言有一个货币数据类型,这将使这个任务变得相当简单。解决这个问题的 pythonic 方法是什么?
Q2. 循环遍历整个购物篮以在结账时生成购物篮中的所有商品,而不仅仅是前 2
我试过了,没用:
for items in basket:
print("Your total bill is:", basket[items][1])
items=items+1
还有这个,也是错误的:
for i in len(basket):
print("Your total bill is:", basket[i][1])
错误:
TypeError: 'int' object is not iterable
这两个问题相互关联,因为它们都与 checkout() 子项有关,其中 addition of all 货币变量在篮子名单中的是objective!
Q1:
考虑到它仅用于基本教学目的,您可以去掉井号(假设表示一致):
def extract_cost(money_in_pounds):
return int(money_in_pounds[1:])
basket = [['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]
total_cost = 0
for items in basket:
total_cost = total_cost + extract_cost(items[1])
print(total_cost)
不写另一个函数
basket = [['Iphone', '£900'], ['Samsung', '£1020'], ['Toshiba', '£700']]
total_cost = 0
for items in basket:
# items[1] selects the cost
# items[1][1:] selects the sub-string of that cost from the 1st index to the end, i.e. remove the currency notation
# int() then converts it into an integer
cost_in_pounds = int(items[1][1:])
total_cost = total_cost + cost_in_pounds
print(total_cost)
更简洁的代码(以下由 Jon Clements 建议)
total_cost = sum(int(cost[1:]) for item_name, cost in basket)
Q2:
items
不是索引,是列表的内容,所以items本身就是内列表:
for items in basket:
print("Your total bill is:", items[1])