我为2或3个输入编写了一个代码但是对于许多输入我应该怎么做

时间:2018-03-06 06:26:05

标签: python python-3.x numpy

print("Store Room Stock Category")

print("")

print("")    

Stockmachinary1 = input("Enter the stock material name:")

Stockmachinary1price=int(input("Enter the stock material price:"))

Stockmachinary2=input("Enter the stock material name:")

Stockmachinary2price=int(input("Enter the stock material price:"))

Stockmachinary3=input("Enter the stock material name:")

Stockmachinary3price=int(input("Enter the stock material price:"))                             



  Totalstockprice=Stockmachinary1price+Stockmachinary1price+Stockmachinary3price

import pandas as pd 

stock = pd.DataFrame({"stock":[Stockmachinary1,Stockmachinary2,Stockmachinary3,"totalcoststock"],\
                          "price":[Stockmachinary1price,Stockmachinary2price,Stockmachinary1price,Totalstockprice]})

stock=stock[["stock","price"]]

stock

Totalstockprice

1 个答案:

答案 0 :(得分:1)

如果你在谈论不写太多代码,我认为你应该使用循环,并且如下所示for循环:

print("Store Room Stock Category")
print("")
print("")
StockmachinaryNames = []
StockmachinaryPrice = []
counts = int(input("Enter the stock material you want input:"))
for i in range(counts):
        Name = input("Enter the stock material name:")
        Price=int(input("Enter the stock material price:"))
        StockmachinaryNames.append(Name)
        StockmachinaryPrice.append(Price)
TotalstockPrice = sum(StockmachinaryPrice)
StockmachinaryNames.append("totalcoststock")
StockmachinaryPrice.append(TotalstockPrice)

import pandas as pd

stock = pd.DataFrame({"stock":StockmachinaryNames,\
                      "price":StockmachinaryPrice})
stock=stock[["stock","price"]]
print(stock)

print(TotalstockPrice)

但是如果你谈论bach数据输入,我认为你可能需要csv或其他文件格式来输入。并且熊猫与它配合得很好。有帮助页面:

  

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html