我的代码中的索引不起作用

时间:2018-01-18 06:50:35

标签: python

#TASK ONE
#       LISTS      #
import sys
import time
cows = []
val = []
num4 = 0
literd = []
test1 = []
val1 = []
day = []
end = []
num2 = []
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
daya = ["1234567"]
# COW ID VALIDATION #
h = int(input("How many cows do you have? "))
for x in range(h):
    Id = int(input("Enter the 3 digit code of your cows: "))
    if len(str(Id)) != 3:
        print("Sorry, the code ID can only be 3 digit numbers!")
        sys.exit()
    elif Id in cows:
        print("The cow ID is already in!")
        sys.exit()
    else:
        cows.append(Id)
###################
# NUMBER OF COW LITERS #
print(" ")
dayz = list(days)
cowz = list(cows)
print(" ")
print("In reality a cow can get milked 25 liters a day, beware!")
for day in days:
    print("Day: ", dayz[0])
    cowz = list(cows)
    dayz.pop(0)
    for cow in cows:
        print("Cow: ", cow)
        test1.append(float(input("How many liters did you milk the cow at AM? ")))
        test1.append(float(input("How many liters did you milk the cow at PM? ")))
val = list(test1)
########################
#ROUNDING NUMBERS AND ADDING THE LITTERS OF COW MILKED#
val = [ round(elem, 1) for elem in test1 ]
kek = list()
total = sum(map(len, daya)) * h
for x in range(total):
    num1 = val[0] + val[1]
    num2.append(num1)
    kek.append(num1)
    for x in range(2):
        val.pop(0)
num3 = list(num2)
########################################################
#               PRINTS OUT THE RESULTS                #
print(" ")
print("A table will print out in just a second showing the results.")
cowz = list(cows)
print(" ")
for day in days:
    print("Day: ", day)
    for cow in cows:
        print("Cow: ", cow, end = " ")
        print("liters: ", num2[0])
        num2.pop(0)
########################################################
cow1 = list()
#TASK TWO
# PRINTING OUT THE TOTAL LITERS OF MILK THIS WEEK#
weekly = list()
num5 = len(num3)
for x in range(num5):
    num4 += num3[0]
    num3.pop(0)
print("Total liters of milk this week", num4)
#################################################
# ADDING ALL THE LITERS FOR EACH COW #
for day in days:
    cowz = list(cows)
    for cow in cows:
        cow1.append(cowz[0])
        cow1.append(kek[0])
        cowz.pop(0)
        kek.pop(0)
##################################################
cow2 = list(cow1)
cow3 = list(cows)
cow_val = []
cow_val1 = []
print(cows, cow3)
for x in range(h):
    current = cow3[0]
    while current in cow3:
       cow_val.append(cow1[cow1.index(current)])
       cow1.pop(cow1.index(current))
       cow1.pop(cow1.index(current)+1)
    cow_val1.append(sum(cow_val))
    cow3.pop(0)

我和我的朋友试图找出问题所在。我们找不到任何其他可能的解决方案,我真的需要这个帮助。这是打印出来的错误

Traceback (most recent call last):
  File "/Users/Datboi/Documents/test.py", line 98, in <module>
    cow1.pop(cow1.index(current)+1)
ValueError: 123 is not in list

我的问题是最后几行。我不知道如何解决这个问题。任何帮助将不胜感激:D。

如果您尝试运行代码并碰巧发现一些错误或任何其他改进,请提出建议。我想尝试让我的代码更短一些,所以是的。

更新编辑。

for x in range(h):
    xo = cow3[0]
    while xo in cow3:
        i += 1
        if x == xo:
            sum1 = sum1 + cow1[i]
    cow_val1.append(sum1)
    cow3.pop(0)

由于某些原因,它不会发生任何事情,因此无效。就像python被打破一样。

4 个答案:

答案 0 :(得分:0)

所以请尝试改变这样:

cow1.pop(cow1.index(current)+1)
cow1.pop(cow1.index(current))

Expl:

在删除第一个元素的代码中,数组的索引会发生变化。然后,当您尝试删除下一个元素时,索引是不同的。所以首先删除下一个元素并尝试删除当前的elemnet

Ed:

for x in range(h):
  current = cow3[0]
  while current in cow3:
      cow_val.append(cow1[cow1.index(current)])
      # Changes
      cow1.pop(cow1.index(current)+1)
      cow1.pop(cow1.index(current))
      # Change here also
      cow3.pop(0)
  cow_val1.append(sum(cow_val))

UP:

c = [333, 3.0, 334, 7.0, 333, 11.0, 334, 15.0, 333, 10.0, 334, 5.0, 333, 9.0,
 334, 13.0, 333, 17.0, 334, 3.0, 333, 7.0, 334, 11.0, 333, 15.0, 334, 10.0] # I think this one you are finally getting

cows = [333, 334] # your list of cows
d = {}
for i in cows:
    d[i] = [] 

for i in range(len(c)):
    if i % 2 != 0:
        d[c[i - 1]].append(c[i])

print(d)
  

o / p:{333:[3.0,11.0,10.0,9.0,17.0,7.0,15.0],334:[7.0,15.0,5.0,13.0,3.0,11.0,10.0]}

print(sum(d[333])) # total milk of cow 333
  

o / p:72.0

答案 1 :(得分:0)

如果我没弄错的话:

cow2 = list(cow1)
cow3 = list(cows)
cow_val = []
cow_val1 = []
print(cows, cow3)
for x in range(h):
    current = cow3[0]
    while current in cow3:
       cow_val.append(cow1[cow1.index(current)])
       cow1.pop(cow1.index(current))
       cow1.pop(cow1.index(current)+1)
    cow_val1.append(sum(cow_val))
    cow3.pop(0)

为什么你在这里弹出两次?如果这是无意的,您可能需要更仔细地查看您的代码。

您可能需要考虑重新组织信息:

     {"cow_idhere":{"sunday":{"AM":0, "PM":0},     \
                    "monday":{"AM":0, "PM":0},     \
                    "tuesday":{"AM":0, "PM":0},    \
                    "wednesday":{"AM":0, "PM":0},  \
                    "thursday":{"AM":0, "PM":0},   \
                    "friday":{"AM":0, "PM":0},     \
                    "saturday":{"AM":0, "PM":0}},
      "nextcow_id": ... }

具有访问权限以获取信息。 (你的部分) 我不认为需要经常弹出并附加如此多的奶牛。我的意思是变量。  示例

def get_MilkForDay(cow_data = None, day=""):
def get_MilkForWeek(cow_data = None):

最后一节(上文)也没有描述它的含义。究竟该怎么做?

答案 2 :(得分:0)

   cow1.pop(cow1.index(current))
   cow1.pop(cow1.index(current)+1)

这是你的错误。你正在为你正在做的事情做太多的流行音乐。停止弹出奶牛!给他们小费!

第一行:你正在做的是寻找值&#34; 333&#34;的索引。让我们说这个列表有[&#34; 333&#34;,&#34; 1&#34;]。弹出第一个元素,现在列表是[&#34; 1&#34;]。
第二行:寻找&#34; 333&#34; - 错误,列表是[&#34; 1&#34;]

我强烈建议您访问信息并计算,而不是不断更改您的信息。使用一些功能,以便您的基本信息保持不变(在某些情况下,它赢了。) 将信息链接在一起,而不是将它们分开并始终重新组合信息。 EG天和大日

days = ["monday", ... "sunday"]
#access days by days[<number>] 
     #days[0] = "monday"
     #days[1] = "tuesday" ... #days[6]="sunday"

你也可以.append()!

milk_Evening = input("milk evening")
milk_Day = input("milk day")

days[0].append(milk_Evening)     #           day[0] [  0   , 1, 2]
days[0].append(milk_Day)         #now it looks like [monday, 1, 3]
#or
#days[0].append([milk_Evening, milk_Day])

星期一挤奶多少钱?     for day_data in days:         如果day_data [0] ==&#34;星期一&#34;:             total_milk = day_data [1] + day_data [2]

注意没有POPPING。收集信息。然后访问信息。没有数据操作。信息从输入时保持不变。

答案 3 :(得分:0)

# ADDING TOTAL LITERS FOR EACH COW #
cow2 = list(cow1)
cow3 = list(cows)
cowval = []
cow_val1 = []
for cow in cows:
    cow2 = list(cow1)
    i = 0
    total1 = 0
    while cow3[0] in cow2:
        if cow3[0] == cow2[0]:
            i += 1
            cowval.append(cow2[i])
            cow2.pop(0)
            cow2.pop(0)
            i = 0
        elif cow3[0] != cow2[0]:
            cow2.pop(0)
            cow2.pop(0)
            i = 0
    cow3.pop(0)
    cow_val1.append(sum(cowval))
    cowval.clear()