python 中if else 语句的作用及示例代码
作者:奶茶,我只要敌敌畏 时间:2023-04-17 00:52:35
引入:if-else的作用,满足一个条件做什么,否则做什么。
if-else语句语法结构
if 判断条件:
要执行的代码
else:
要执行的代码
判断条件:一般为关系表达式或bool类型的值
执行过程:程序运行到if处,首先判断所带的条件,如果条件成立,就是返回值是True,则执行下面的代码;如果条件不成立则返回值是False, 则继续执行下面的代码。
示例1:模拟用户登录
提示输入用户名和密码
如果用户名是Admin,密码等于123.com, 提示用户登录成功
如果用户名不是Admin,提示用户不存在
如果密码不等于123.com, 提示密码错误
username= input("请输入用户名:")
password = input("请输入密码:")
if username.lower().strip()== "admin" and password == "123.com":
print("登录成功!")
else:
print("用户名或者密码错误!")
# lower()--把字符串转为小写 upper()--把字符串转为大写
# strip()--去除字符串前后的空格
结果:
C:\python\python.exeC:/python/demo/file2.py
请输入用户名: ADMIN
请输入密码:123.com
登录成功!
Process finished with exit code 0
示例2:
在控制台应用程序中输入小王(语文,英语,数学)成绩(单科满分100分)
判断:
1)如果所有科目都及格了,提示:恭喜你,你所有科目都通过考试了
2)否则提醒:很遗憾,你没有通过考试,需要补考(没有及格的名称)
chinese= int(input("请输入语文成绩:"))
maths = int(input("请输入数学成绩:"))
english = int(input("请输入英语成绩:"))
get_course = ""
if chinese>= 60 and maths >= 60 and english>= 60:
print("恭喜你,所有科目都通过考试!")
else:
if chinese <= 60:
get_course += "语文、"
if maths <= 60:
get_course += "数学、"
if english <= 60:
get_course += "英语、"
print("很遗憾,你没有通过考试,补考科目为:" + get_course)
结果:
C:\python\python.exeC:/python/demo/file2.py
请输入语文成绩:54
请输入数学成绩:89
请输入英语成绩:32
很遗憾,你没有通过考试,补考科目为:语文、英语、
Process finished with exit code 0
示例3:(之前小红花案例第一次优化)
在控制台应用程序中输入小王(语文,英语,数学)成绩(单科满分100分)
判断:
1)如果有一门是100分
2)如果有两门大于90分
3)如果三门大于80分
满足以上一种情况,则奖励一朵小红花
chinese= int(input("请输入语文成绩:"))
maths = int(input("请输入数学成绩:"))
englist = int(input("请输入英语成绩:"))
get_course = ""
if (chinese == 100 or maths == 100 or englist == 100):
if(chinese == 100):get_course += "语文、"
if(maths == 100):get_course += "数学、"
if(englist == 100):get_course += "英语、"
print("你的%s得了100分,奖励一朵小红花❀!" % get_course)
else:
if(chinese >= 90 and maths >=90) or (chinese >= 90 and englist>= 90) or(maths >= 90and englist >= 90):
if(chinese >= 100):get_course += "语文、"
if(maths >= 90):get_course += "数学、"
if(englist >= 90):get_course += "英语、"
print("你的%s大于90分,奖励一朵小红花❀!" % get_course)
else:
if(chinese >= 80 and maths >=80 and englist >= 80):
print("你的三个科目语文、数学、英语都大于80分,奖励一朵小红花❀")
结果:
C:\python\python.exeC:/python/demo/file2.py
请输入语文成绩:86
请输入数学成绩:98
请输入英语成绩:87
你的三个科目语文、数学、英语都大于80分,奖励一朵小红花❀
总结
以上所述是小编给大家介绍的python 中if else 语句的作用及示例代码网站的支持!
来源:https://www.cnblogs.com/wangjin-ilync/archive/2018/03/05/8507439.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
记Qzone项目组视觉设计标注的前前后后
![](https://img.aspxhome.com/file/UploadPic/20103/24/1321290-25s.jpg)
MySQL的安全问题从安装开始说起
HTML邮件的又一点思考
![](https://img.aspxhome.com/file/UploadPic/20095/6/mail-59s.png)
jquery与google map api结合使用 控件,监听器
优雅管理Go Project生命周期
![](https://img.aspxhome.com/file/2023/0/87630_0s.jpg)
python 输出所有大小写字母的方法
PHPExcel笔记, mpdf导出
python GUI计算器的实现
![](https://img.aspxhome.com/file/2023/8/87948_0s.png)
python deque模块简单使用代码实例
通过numba模块给Python代码提速的方法详解
![](https://img.aspxhome.com/file/2023/0/77880_0s.jpg)
Python爬虫代理池搭建的方法步骤
![](https://img.aspxhome.com/file/2023/3/105733_0s.png)
ASP函数大全(数字函数)
兼容Firefox的点击复制js代码
Python列表去重的几种方法整理
![](https://img.aspxhome.com/file/2023/8/120468_0s.png)
Python数据模型与Python对象模型的相关总结
dl+ol应用
python通过http下载文件的方法详解
Python通过for循环理解迭代器和生成器实例详解
Django-Scrapy生成后端json接口的方法示例
![](https://img.aspxhome.com/file/2023/4/97094_0s.png)