繁体   English   中英

为什么我的代码打印不同,然后它在 function 中返回?

[英]Why does my code print different then it returns in a function?

代码:

Encode_Decode = input("Would you like to Encode, or Decode your text?")


def encode_decode_retry():
    an = input('Sorry, I didn\'t understand. Would you like to Encode, or Decode your text?')
    if an != "Encode" and an != "Decode":
        an = ""
        encode_decode_retry()
    else:
        print(an)
        return an


if Encode_Decode != "Encode" and Encode_Decode != "Decode":
    x = (encode_decode_retry())
    print(x)

它打印“解码”或“编码”,但是当我返回它时,它返回“无”。 我的版本是3.7

您在 if 子句中缺少 return 语句。 只需在调用 function 之前添加 return

def encode_decode_retry():
    an = input('Sorry, I didn\'t understand. Would you like to Encode, or Decode your text?')
    if an != "Encode" and an != "Decode":
        an = ""
        return encode_decode_retry()
    else:
        print(an)
        return an

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM