Why am I getting this error in python when i enter into next line of if-else condition?

前端 未结 2 998
無奈伤痛
無奈伤痛 2021-01-29 09:34
if cake == \"delicious\":
    return \"yes\"

SyntaxError: \'return\' outside function

Why I get like this?

相关标签:
2条回答
  • 2021-01-29 10:08

    return is a keyword which can only appear in a function definition (as the syntax error says). You can simply print your output in an if/else block

    if cake == 'delicious': print('yes')   
    
    0 讨论(0)
  • 2021-01-29 10:17

    You can create some functions like this.

    text  ="delicious" 
    def check(text):
        if text=="delicious":
            return 'yes'
    
    check(text)
    
    0 讨论(0)
提交回复
热议问题