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 2> =5输出:总价= 200 2> id = 103 2>id= 20输出;缺货“”
发布于 2021-10-08 11:18:39
这里是最后的解决方案
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")发布于 2021-10-06 10:50:43
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)https://stackoverflow.com/questions/69463988
复制相似问题