python - 如何在python中添加if else语句以及函数定义
问题描述
很抱歉打扰我刚开始学习python的编码。我只是在python中自己创建了一些代码,没有错误显示。但是为什么 if else 语句没有执行。有人可以帮忙吗?
代码:
def name(f_name, age):
return f"Hi, {f_name} {age}"
if age <= 18:
print("You are Not eligeble for The post")
else:
print("you are eligeble for post")
person = name("Aditya", 17)
print(person)
解决方案
在您的第 2 行,您使用了“return”。这意味着下面的代码不会被执行。您应该将代码行 2 移动到函数的末尾,例如
def name(f_name, age):
# this code has removed
if age <= 18:
print("You are Not eligeble for The post")
else:
print("you are eligeble for post")
return f"Hi, {f_name} {age}"
推荐阅读
- python - 二元分类问题的低准确率
- java - JavaFX 无法从资源文件夹加载图像
- python - 使用 imshow (OpenCV) 显示图像时出现问题
- flutter - java.lang.IllegalArgumentException:服务未注册:lp@f7269a6
- amazon-web-services - 如何使用 AWS Lambda 节点函数插入 mysql,然后将 JSON 返回到 websocket 客户端
- javascript - Mongoose Pre 和 Post 钩子:停止执行查询
- node.js - 如何在 nodejs express 应用程序初始化时插入数据
- sql - 编写查询以查找视频租赁组织收入最高的电影
- vb.net - 根据组合框中所选项目的索引从排序列表中检索值
- javascript - 是否可以在 api 端点的末尾实现一个 cachebreaker 以便在 Javascript 中重新加载 jpeg?