【问题标题】:Python IDLE error in an outside function外部函数中的 Python IDLE 错误
【发布时间】:2018-08-22 14:40:43
【问题描述】:

有一个外部函数,但我不知道在哪里。 IDLE 说它在第 37 行。当我移动 return 语句时,Python 说它是缩进的,但有时它说它是一个外部函数。 我正在查看 Python 手册以了解详细信息。

def move () :
    while True:
        print
        player = raw_imput ("Pierre = 1 \nPapier = 2\nCiseaux = 3\nQuel est ton choix?: ")
    try:
        player = int(player)
        if player in (1,2,3):
            return player
    except (ValueError):
        pass   
    print ( "Mince ! Je n'ai pas compris. S'il vous pla?t entrer 1,2 ou 3. " )

【问题讨论】:

  • 能否请您发布 IDE 抱怨的确切错误?另外:代码中的哪一行是第 37 行?
  • 谢谢,但我修复了错误
  • 如果您已经解决了这个问题,请考虑关闭/删除它。这似乎微不足道,对 SO 社区没有任何价值。
  • 还可以考虑发布您的修复作为您自己问题的答案。
  • 格式化代码,增加清晰度

标签: python function return


【解决方案1】:

您的代码已破坏缩进。例如,try: 需要缩进,否则函数的作用域将结束,return 稍后将似乎在函数之外。

def move () :
    while True:
        print
        player = raw_imput ("Pierre = 1 \nPapier = 2\nCiseaux = 3\nQuel est ton choix?: ")
try:
#^ This try here is on the same level as "def". This means that it is _outside_ the function.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多