【问题标题】:Python Error Calling Function Within Function [closed]函数内的Python错误调用函数[关闭]
【发布时间】:2016-07-18 06:08:54
【问题描述】:

我是python的新手。我刚刚扩展了 Hello World 程序:

import sys

def first(n):
    print "Hi Man"+" Huuuuuu"
    if len(sys.argv)>1: print sys.argv[1]

def second():
    print "Huuuuuuuuuuu"    
    first(7)

if __name__=='__main__':
    second()

我得到错误:

File "first.py", line 11
    first(7)
    ^
IndentationError: unexpected indent

我阅读了What to do with "Unexpected indent" in python?,但找不到错误。

我用 python -t 命令运行:

python -t first.py
first.py: inconsistent use of tabs and spaces in indentation
  File "first.py", line 12
    first(7)
    ^
IndentationError: unexpected indent

感谢您的帮助。

【问题讨论】:

  • 我复制粘贴您的代码并且没有错误。您确定您共享的代码正是您正在运行的代码吗?
  • 确保不要混合使用 4 个空格和制表符。
  • 在第 12 行的代码中几乎可以肯定存在缩进错误(因此是 IndentationError)。在函数中调用函数不是您的问题。修复缩进。
  • @JonSurrell 我刚刚从我的问题中复制了代码,它有效吗?它以前不起作用。
  • 好像你在混合制表符和空格。

标签: python python-2.7 error-handling


【解决方案1】:

Python 通常通过缩进而不是花括号块来工作。在您的情况下,检查 first(7) 语句前面的空格是否与第一个 print 语句的空格匹配。

检查您是否已将制表符放入其中一个语句中。一般我们把4个空格放在语句前面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2020-08-25
    相关资源
    最近更新 更多