TCS DCA问题:python中的迷你杂货店


import pandas as pd
id_num=[101,102,103,104]
price = [40,50,60,70] 
stock = [10,14,14,13] 
new = pd.DataFrame(
{'id_num':id_num,
'price':price,
'stock':stock
})
try:
inp_num=int(input("enter the id number:")) 
qua = int(input("enter the quantity:")) 
except ValueError:
print("Invalid")
if([new['id_num']==inp_num]):
total = price*qua
print(total)

解释程序:输入客户想要购买的id和股票值,并根据数量计算价格示例输入:1>Id = 101数量= 5个输出:总价= 200美元>Id = 103数量= 20输出;脱销">

这是最终的解决方案

id_num=[101,102,103,104]
price = [40,50,60,70] 
stock = [10,14,14,13] 
try:
inp_num=int(input("enter the id number:")) 
qua = int(input("enter the quantity:")) 
except ValueError:
print("Invalid")

if inp_num in id_num:
ind = id_num.index(inp_num)
if qua <= stock[ind]:
total = price[ind]*qua
stock[ind] -= qua
print(total)
else:
print("out of stock")
else:
print("no product available")
import pandas as pd
def new_funct(new):
try:
inp_num=int(input("enter the id number:")) 
qua = int(input("enter the quantity:")) 
except ValueError:
print("Invalid")

list_of_value = new['id_num'].tolist()
if inp_num in list_of_value:
index_prod = list_of_value.index(inp_num)

if qua <= new['stock'][index_prod]:
total = new['price'][index_prod]*qua
new.at['stock', index_prod]  = new['stock'][index_prod] - qua
print("total price =",total)
else:
print("out of stock")
else:
print("no product available")
return new
id_num=[101,102,103,104]
price = [40,50,60,70] 
stock = [10,14,14,13] 
new = pd.DataFrame({'id_num':id_num,'price':price,'stock':stock})
while True:
new_funct(new)

最新更新