【问题标题】:Lists, Loops, Average Python列表、循环、普通 Python
【发布时间】:2014-11-22 17:45:03
【问题描述】:

我的代码目前有问题。该程序工作正常,但由于某种原因,当我输入所有正数时,我收到最后一个函数的错误。最后一个函数是从列表中删除所有负数。

i=0
numList = []
x = int(input("Please enter number from -9999 to end:"))
while x > -9999:
    i = i + 1
    numList.insert(i,x)
    x = int(input("Please enter number from -9999 to end:"))
    if x == -9999:
    print("The list of numbers entered:",(numList))

newList = numList[:]
secList = numList[:]

def posNumAvg(newList):
    for e in newList[:]:
        if e < 0:
            newList.remove(e)            


def negNumAvg(secList):
    for y in secList[:]:
        if y > 0:
            secList.remove(y)            

posNumAvg(newList)
negNumAvg(secList)
mydict = {'AvgPositive':(sum(newList)//len(newList)),'AvgNonPos':     (sum(secList)//len(secList)),'AvgAllNum':(sum(numList)//len(numList))}
print("The dictionary with averages is:",mydict)

【问题讨论】:

  • 你遇到了什么错误?
  • 那么程序不能正常工作:)

标签: python list loops python-3.x dictionary


【解决方案1】:

您正在尝试计算未定义的空列表的平均值(具体而言,列表的长度为零,因此您最终会被零除)。

你需要决定在这种情况下你想做什么并具体处理它。

顺便说一句,你可以重写

for e in newList[:]:
    if e < 0:
        newList.remove(e)            

作为

newList = [e for e in newList if e >= 0]

【讨论】:

    【解决方案2】:

    注意:迭代列表时切勿修改列表。让我对您的代码进行一些更改

    numList = []
    while True: 
        x = int(input("Please enter number from -9999 to end:")
        if x == -9999:   
            print("The list of numbers entered:",(numList))
            break
        # you don't need i here, used append to insert element at last of list
        # insert is used to insert a element at specific position
        numList.append(x)
    
    def posNumAvg(gotList):
        list_negative = []   # will contain negative number
        for e in gotList:
            if e < 0:
                list_negative.append(e)
        return list_negative
    
    def negNumAvg(gotList):
        list_positive = []   # will contain positive number
        for y in gotList:
            if y >= 0:
                list_positive.append(y)            
        return list_positive
    
    list_positive = posNumAvg(numList)
    list_negative = negNumAvg(numList)
    mydict = {'AvgPositive':(sum(list_positive)//len(list_positive)),'AvgNonPos':     (sum(list_negative)//len(list_negative)),'AvgAllNum':(sum(numList)//len(numList))}
    print("The dictionary with averages is:",mydict)
    

    您可以从一个函数中获取正数和负数:请参阅此代码

    def neg_poss(gotList):
        list_positive = []   # will contain positive number
        list_negative = []   # will contain negative number
        for y in gotList:
            if y >= 0:
                list_positive.append(y)            
            else:list_negative.append(y)
        return list_positive,list_negative
    
    list_positive,list_negative = neg_post(numList)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 2016-02-25
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      相关资源
      最近更新 更多