data = [['01','Book',5],['02','Pen',2],['03','Pencil',1.5],['04','Paper',15],['05','USB Drive',20]]
提示用户输入产品名称和数量。根据需要重复多次。 计算并显示用户输入的产品的数量,名称和价格。 计算并显示购买这些产品的总费用。
more = 'yes'
shopping_list = []
while more == 'yes':
user_input = input('Enter a product:')
user_quantity = int(input('Enter the number of products:'))
shopping_list.append(user_input)
shopping_list.append(user_quantity)
more = input('Would you like more?')
我的代码不起作用。
答案 0 :(得分:1)
我将格式化数据以将其保存在字典中,其中唯一键为product_name,值为价格
mtcars <- as.data.table(mtcars)
returnNames <- "calculatedColumn"
SDnames <- c("mpg","hp")
myfunc <- function(data) {
print(data)
return(data[,1]*data[,2])
}
functionCall <- "myfunc"
格式化的数据如下:
{'Book':5,'Paper':15,'Pen':2,'Pencil':1.5,'USB Drive':20}
formatted_data={j:k for i,j,k in data}
答案 1 :(得分:0)
data = [['01','Book',5],['02','Pen',2],['03','Pencil',1.5],['04','Paper',15],['05','USB Drive',20]]
data_dict = {i[0]:[i[1],i[2]] for i in data}
shopping_list = {}
while True:
try:
user_input = input('Enter a product:')
user_quantity = int(input('Enter the number of products:'))
shopping_list[user_input] = [data_dict[user_input][0], user_quantity,user_quantity*data_dict[user_input][1]]
more = input('Would you like more?')
if more == 'no':
break
except KeyError:
print('We dont have this ID. Try again')