首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TCS DCA问题: python的迷你杂货店

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

Stack Overflow用户
提问于 2021-10-06 10:26:59
回答 2查看 183关注 0票数 1
代码语言:javascript
复制
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输出;缺货“”

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-08 11:18:39

这里是最后的解决方案

代码语言:javascript
复制
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")
票数 0
EN

Stack Overflow用户

发布于 2021-10-06 10:50:43

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69463988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档