此文为笔者对python学习的巩固记录,如有错误,欢迎指正,希望在学习的路上与君共勉
-
1.数学的流程图
-
2.账号,密码的正确性问题
等这些问题都属于分支结构
一、分支结构涉及到的python关键字:if,else,elif
二、语法:单分支结构(二选一),多分支结构(多选一)
三、单分支结构
登录注册功能
username = 'zs'
password = '123456'
遇到某行代码末尾出现 : ,下一行有4个空格的缩进
if username == 'zs' and password == '123456':
print("登录成功")
else:
print("登录失败")
'''
语法:
if 条件(布尔值):
代码块1
else:
代码块2
如果if后面的条件成立(True),执行代码块1,反之执行代码块2
'''
-
1.判断一个年份是否为闰年,返回闰年或者平年
-
2.判断一个人是否成年
y=int(input("输入年份:"))
if y%4==0 and y%100!=0 or y%400==0:
print("闰年")
else:
print("平年")
age=int(input("输入年龄:"))
if age>=18:
print("成年")
else:
print("未成年")
四、多分支结构
0-100的成绩划分为A,B,C,D,E
score = 66
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("E")
'''
语法:
if 条件1:
代码块1
elif 条件2:
代码块2
······
else:
代码块N
'''
改写判断一个年份是否为闰年为多分支结构
y = 2000
if y%4==0 and y%100!=0:
print("闰年")
elif y%400==0:
print("闰年")
else:
print("平年")
a = y
if a%4==0:
if a%100!=0:
print("闰年")
elif a%400==0:
print("闰年")
else:
print("平年")
else:
print("平年")