这是我的代码,我只想让它读取输入,如果不是空的,只需返回输入的内容。

print "Welcome to translator!"
print "Enter in an english word"
original=raw_input("Enter a word")

if len(original) != 0:
     return original

最佳答案

您只能从函数中return生成SyntaxError
实现你想要的东西的正确方法是:

print "Welcome to translator!"
print "Enter in an english word"
original = raw_input("Enter a word")

if original: # Checks if original is not empty
     print original

如果要从函数返回:
print "Welcome to translator!"
print "Enter in an english word"
original=raw_input("Enter a word")

def not_empty(string):
     if string:
        return string

print not_empty(original)

关于python - 返回外部函数错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20592667/

10-12 16:46