为什么我的程序出错

时间:2018-03-02 21:25:17

标签: python

我需要帮助完成我的程序,我试图找到最高和最低的收益率,但不知道如何编写此代码。到目前为止这是我的程序

Cow = []
Yield = []
Total = []

Cows = int(input("How many cows are in the heard?"))
    Day = 0
    for i in range(14):
        if i%2 == 1:
            Milking = "Second"
        else:
            Milking = "First"
            Day += 1
        print("Day ", Day, ";", Milking, "milking")
        for i in range(Cows):
            while True:
                Code = int(input("Enter code: "))
                if Code > 999 or Code <100:
                    print("Enter a 3 digit code")
                else:
                    Cow.append(Code)
                    break
            Y = float(input("Enter volume of milk in litres: "))
            Yield.append(Y)

    Total = 0
    for i in range(len(Yield)):
        Total += Yield[i]
    Average = Total/Cows
    round(Total, 0)
    round(Average, 0)
    print("Total weekly volume of milk: ", int(Total), "Litres")
    print("Average yield per cow: ", int(Average), "litres")

到目前为止一切正常,当程序达到这一点时我收到错误。

for i in range(len(Cow)):
    if Cow[i] == Cow:
        T += Yield[i]
        YieldOnDay += Yield[i]
        Milking += 1
        if Milking == 2:
            if YieldOnDay < 12:
                LessMilk = LessMilk + str(Cow[j]) + ", "
    Total.append(T)

print(Total)
for i in range(Cows):
    if Total[i] == max(Total):
        print("Cow ", Cow[i], "has the highest yield of ", Total[i], "Litres")

print("Cows which produced less than 12 litres of milk: ", LessMilk)

错误说

for i in range(len(Cow)):
TypeError: object of type 'int' has no len()

2 个答案:

答案 0 :(得分:0)

for i in range(len(Cow)): ## Go through all the cows (using indexing)
    if Yield[i] < 12: ## Is the Yield less than 12 (litres) ?
        print("Under 12 Litres:", Cow[i]) ## If yes, print out the cow code and its yield

m = max(Yield) ## Get the max yield
print("Max:", Cow[Yield.index(m)]) ## Get the code of the cow with the max yield by indexing and print it out, as well as its yield

答案 1 :(得分:0)

使用字典存储奶牛的产量。

CowYields = dict()
CowYields['cow1']= 100
CowYields['cow2']=200
CowYields
{'cow1': 100, 'cow2': 200}

MaxYield = max(CowYields.values())
200
MaxCow = max(CowYields, key=CowYields.get)
AVG = sum(CowYields.values())/len(CowYields)

#12 lit
for key in CowYields.keys():
     if CowYields[key] < 12:
        print(CowYields[key])