【问题标题】:Python 2.7 Issues running functions with listsPython 2.7 使用列表运行函数的问题
【发布时间】:2015-11-17 01:29:35
【问题描述】:

在 Python 2.7 中,我的函数 pop(y,p) 的代码完美运行,但函数 aveChange(p,y) 返回多个打印行,我需要它只打印 1 行表示人口年平均变化的行。

而函数change(p,y,b) 只是说:

NameError: name 'b' is not defined

到目前为止我的代码如下:

def biggerThan():
    l=raw_input('Enter a list of numbers, separate each number with a    comma:').split(',')
    a=map(int,l)
    n=int(raw_input('Enter another number:'))
    a2=[i for i in a if i>n]
    print a2

    p = [151868, 153982, 156393, 158956, 161884, 165069, 168088, 
    171187, 174149, 177135, 179979, 182992, 185771, 188483, 
    191141, 193526, 195576, 197457, 199399, 201385, 203984, 
    206827, 209284, 211357, 213342, 215465, 217563, 219760, 
    222095, 224567, 227225, 229466, 231664, 233792, 235825, 
    237924, 240133, 242289, 244499, 246819, 249623]
    y1=1950
    y2=1991
    y=range(y1,y2)
def pop(y,p):
    fmt='{:<8}{:<20}{}'
    print(fmt.format('', 'Year', 'Population'))
    for i, (year, pop) in enumerate(zip(y, p)):
        print(fmt.format(i, year, pop))

def aveChange(p,y):
    p = [151868, 153982, 156393, 158956, 161884, 165069, 168088, 
    171187, 174149, 177135, 179979, 182992, 185771, 188483, 
    191141, 193526, 195576, 197457, 199399, 201385, 203984, 
    206827, 209284, 211357, 213342, 215465, 217563, 219760, 
    222095, 224567, 227225, 229466, 231664, 233792, 235825, 
    237924, 240133, 242289, 244499, 246819, 249623]
    y1=1950
    y2=1991
    y=range(y1,y2)
    yearly_change = []
    change=0.0
    total_change=0
    average_change=0
    for i in range(len(p)):
        p[i] = float(p[i])
        #calculate the change in population size for each two years
    for i in xrange(1,len(p)):
        change = p[i] - p[i-1]
        yearly_change.append(change) 
        total_change = float(sum(yearly_change))
        average_change = total_change/40
        print "The average annual change in population during the time period is",average_change

def change(p,y,b):
    p = [151868, 153982, 156393, 158956, 161884, 165069, 168088, 
    171187, 174149, 177135, 179979, 182992, 185771, 188483, 
    191141, 193526, 195576, 197457, 199399, 201385, 203984, 
    206827, 209284, 211357, 213342, 215465, 217563, 219760, 
    222095, 224567, 227225, 229466, 231664, 233792, 235825, 
    237924, 240133, 242289, 244499, 246819, 249623]
    y1=1950
    y2=1991
    y=range(y1,y2)
    yearly_change = []
    newchange=0.0
    greatest_increase=0
    smallest_increase=0
    greatest_year=0
    smallest_year=0
    BASE_YEAR=1950
    for i in range(1,len(p)):
        newchange = p[b] - p[b-1]
        yearly_change.append(newchange)

        if b==1:
            greatest_increase = newchange
            smallest_increase = newchange
            greatest_year = 1
            smallest_year = 1
        else:
            if newchange>greatest_increase:
                greatest_increase = newchange
                greatest_year = b
            elif newchange<smallest_increase:
                smallest_increase = newchange
                smallest_year = b
            print("The year with the greatest increase in population was",
                BASE_YEAR+greatest_year)
            print("The year with the smallest increase in population was",
                BASE_YEAR+smallest_year)

【问题讨论】:

  • 您帖子中的换行符是否也在您的代码中?
  • 执行此操作真的不会为我带来任何NameError
  • 也许你只需要更小的缩进符合print
  • 我刚刚尝试减少打印缩进,但仍然出现错误:NameError: name 'b' is not defined

标签: python python-2.7


【解决方案1】:

将您的 print 语句移到 for 循环之外的 aveChange(p,y)

for i in xrange(1,len(p)):
    change = p[i] - p[i-1]
    yearly_change.append(change) 
    total_change = float(sum(yearly_change))
    average_change = total_change/40
print "The average annual change in population during the time period is ",average_change

对于 change(p, y, b) 函数,请确保您使用 3 个参数调用它,并且您的所有参数都已定义!您收到错误是因为您使用b 调用函数,但在调用之前未在函数外部定义b!尝试这样称呼它:

change(p, y, 5)

【讨论】:

  • 这确实修复了 aveChange() 函数。非常感谢,这让我发疯了。我确实确保我正确调用了 change() 函数,但它仍然给我同样的错误。我假设我需要更好地定义 b 但我只是想念它。
  • 这将有助于展示您如何调用 change()。当我们只是复制粘贴代码并自己调用时,我们中的几个人似乎可以很好地运行代码。构建时可能没有使用 2.7 吗?
  • 我叫作 change(p,y,b)
  • 我相信我在 2.7 中。我正在使用 Canopy 进行编辑,但我不确定如何检查它是使用 2.7 还是 3
  • @Mike 我已经更新了我的答案,它应该可以完全解决你的问题,现在使用信息。根据您的上述评论。
猜你喜欢
  • 1970-01-01
  • 2014-05-13
  • 2016-06-09
  • 1970-01-01
  • 2013-05-11
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多