python - 为什么我的代码打印不同,然后在函数中返回?
问题描述
代码:
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 语句。只需在调用函数之前添加 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
推荐阅读
- android - Apk 不是科尔多瓦项目的构建
- c# - 在不临时创建字典的情况下反序列化为 List 而不是 Dictionary
- https - 无法从 Webshphere 应用程序服务器 v7.0 上托管的应用程序访问 HTTPS 服务
- javascript - jquery ajax - invalid parametar number
- entity-framework - Azure 函数连接字符串部署问题
- javascript - 使用 es6 map 更改数组中元素的特定属性
- javascript - 使用 Ajax、Jquery 上传文件
- azure-machine-learning-studio - Azure 机器学习工作室将行追加到数据集
- javascript - 第一次渲染 Handlebars 模板,但还要保留它们以供后续渲染?
- xamarin - Label VerticalOptions 和 VerticalTextAlignment 有什么区别?