python3 TypeError:“函数”对象不可迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22633530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:19:06  来源:igfitidea点击:

python3 TypeError: 'function' object is not iterable

pythonfunctionpython-3.xiteration

提问by Droid

What change is required in the source code?

源代码需要什么改动?

    def Update():
        print('\n')
        print("Update")
        cmd = os.system('xterm -e apt-get update')
        print("Finish update")

    def AptUpdate():
        print('\n')
        print("Update system? {Y/N}")
        print("Y or y")
        print("N or n")
        code = input("Command > ")
        if code == 'y' or code == 'Y':
            for i in Update():
                return Update
            elif code == 'n' or code == 'N': 
                return 
            else: 
                print("Warning!")

    AptUpdate()

    exception:

    Traceback (most recent call last):
      File "pybash.py", line 110, in 
        AptUpdate()
      File "pybash.py", line 102, in AptUpdate
        for i in Update:
    TypeError: 'function' object is not iterable

采纳答案by user3460822

What the traceback error is pointing out is the misuse of for statement:

回溯错误指出的是 for 语句的误用:

for i in Updt():

for i in Updt():

forin python 3 is as follows: "Python's for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence." (source: python 3.3 documentation, section 4: More control structures Python 3

for在 python 3 中如下:“Python 的 for 语句迭代任何序列(列表或字符串)的项目,按照它们在序列中出现的顺序。” (来源:python 3.3 文档,第 4 节:更多控制结构Python 3

Since a function is neither a list nor a string, you can't use the format:

由于函数既不是列表也不是字符串,因此您不能使用以下格式:

for [variable] in [function]():

for [variable] in [function]():

As far as what needs to be fixed, it depends on what those two functions are supposed to accomplish individually.

至于需要修复什么,这取决于这两个功能应该单独完成什么。