照抄的代码运行出错 麻烦高手给把把关

来源:11-16 再用闭包解决一下_

慕用9559375

2019-09-02

origin = 0

def factory(pos):
def go(step):
nonlocal pos
new_pos = pos + step
pos = new_pos
return new_pos
return go
tourist = factory(origin)
print(tourist(2))

照抄的代码 运行出错
localhost:sslee garywork$ python page9.py
File “page9.py”, line 5
nonlocal pos
^
SyntaxError: invalid syntax

写回答

2回答

慕仙1067096

2021-05-24

是 return pos。

0
0

Sayno_2

2019-09-03


origin = 0
def factory(pos):
    def go(step):
        nonlocal pos
        new_pos = pos + step
        pos = new_pos
        return new_pos
    return go
tourist = factory(origin)
print(tourist(2))

python代码用缩紧控制代码执行顺序

0
1
九十九杯咖啡
你确定是照抄的吗 你好像多返回了一个
2020-02-21
共1条回复

Python3.8系统入门+进阶 (程序员必备第二语言)

语法精讲/配套练习+思考题/原生爬虫实战

14773 学习 · 4487 问题

查看课程