为什么我的代码输出不同,然后返回函数?

时间:2019-09-26 02:50:07

标签: python python-3.x

代码:

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

1 个答案:

答案 0 :(得分:2)

您在if子句中缺少return语句。只需在调用函数之前添加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