如何解决“返回”外部功能错误

时间:2018-07-23 08:46:22

标签: python python-3.x

phone_letters =[" ", "1", "ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ","*","0","#" ]
key = 0
string = input("Enter a Letter: ",)

while key < 10 :
        if string in phone_letters[key]: 
            print(key)
            return key
        else:
            key = key+1

        return "not found"

我遇到错误'return' outside function;我检查了缩进,但错误仍然存​​在。

1 个答案:

答案 0 :(得分:1)

  

return可能仅在语法上嵌套在函数定义中

来源:https://docs.python.org/3/reference/simple_stmts.html#the-return-statement

您的返回未嵌套在函数内。因此,错误。

如果要显示文本,可以使用print("not found")