python3 TypeError:'function'对象不可迭代 [英] python3 TypeError: 'function' object is not iterable

查看:784
本文介绍了python3 TypeError:'function'对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 
$ b $更新():
print(' \ n')
print(Update)
cmd = os.system('xterm -e apt-get update')
print(完成更新)

def AptUpdate():
print('\\\
')
print(Update system?{Y / N})
print(Y or y)
print(N or n)
code = input(Command>)
if code =='y'or code =='Y':
for i在Update()中:
返回更新
elif code =='n'或代码=='N':
return
else:
print(Warning! )

AptUpdate()

例外:

Traceback(最近一次调用最后一次):
文件pybash.py,第110行,在
AptUpdate()
在AptUpdate
中的文件pybash.py,第102行,更新中的i:
TypeError:'function'对象不可迭代


解决方案

什么是回溯错误指出是滥用for语句:



for我在Updt()中:

$ b在Python 3中,
$ b

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



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








$ b $ 需要解决的问题,取决于这两项功能应该单独完成的功能。

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

解决方案

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

for i in Updt():

for in 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

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

for [variable] in [function]():

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

这篇关于python3 TypeError:'function'对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆