【问题标题】:Python3 for loop acting un-expectedlyPython3 for 循环意外地起作用
【发布时间】:2017-09-20 08:23:03
【问题描述】:

当我遇到这个时,我正在制作一个程序:

File "intcrypt.py", line 11
    if(item == 9):
             ^
IndentationError: unindent does not match any outer indentation level

我真的很困惑这怎么可能?对我来说似乎很好,我已经多次编写过这样的程序,它们都运行得很顺利。对于所有逻辑,我非常确定缩进看起来是正确的。可能是什么问题?

这是我的代码:

num = 23983209

mylist = list(map(int,str(num)))

anotherlist = []

for item in mylist:

    item += 0

    if(item == 9):

        print("Item == 9!")

    anotherlist.append(item)

print(anotherlist)

提前致谢!

【问题讨论】:

  • 检查您是否有混合的空格和制表符。你也可能是指anotherlist.append(item)。剪切和粘贴上面提到的更正的代码使代码工作,所以上面的代码不会重现缩进错误。
  • 谢谢!一切都好

标签: python-3.x for-loop


【解决方案1】:

如果我将程序复制粘贴到此interpret 中,则不会出现 indetation 错误。但是,附加到列表中存在错误。应该是anotherlist.append(item)

【讨论】:

    【解决方案2】:

    这是

        anotherlist.append(item) 
    

    不是

        item.append(anotherist)
    

    正确的代码

    num = 23983209
    
    mylist = list(map(int,str(num)))
    
    print mylist
    anotherlist = []
    
    for item in mylist:
    
        item += 0
    
        if(item == 9):
    
            print("Item == 9!")
    
        anotherlist.append(item) #problem in this line
    
    print(anotherlist)
    

    【讨论】:

    • 这并没有回答问题,并且提到的错误不会产生问题中的错误。
    • 这不是问题,但我确实修复了该代码。谢谢!
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多