为什么我的程序出错

时间:2021-09-03 19:43:14

I need help completing my program, I'm trying to find the highest and lowest yield but don't know how to write this code. This is my program so far

我需要帮助完成我的程序,我正在尝试找到最高和最低的产量,但不知道如何编写此代码。到目前为止,这是我的计划

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")

everything works well till this point, I get an error when the program reaches this point.

一切顺利,直到这一点,当程序到达这一点时,我得到一个错误。

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)

the error says

错误说

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

2 个解决方案

#1


0  

Use a dictionary to store yield of cows.

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

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]) 

#2


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  

Use a dictionary to store yield of cows.

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

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]) 

#2


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