Files
2025PY/day07/02-if语句大练习.py
2025-05-22 16:50:44 +08:00

46 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 1.用户输入用户名:zhangsan、密码:123456则提示登入成功否则提示失败
# 过程
# 当我们通过表单输入用户名和密码,点击提交或者登录
# 用户名和密码传递给后端(服务器端)
# 服务器端拿到用户名和密码
# 将用户名和密码和你注册时的用户名和密码进行匹配
# 双分支实现
# user_name = input('请输入用户名:')
# user_password = input('请输入密码:')
# if user_name == 'zhangsan' and user_password == '123456': # 必须两个条件同时满足
# print('登录成功')
# else:
# print('登录失败,请重新输入用户名和密码')
# 2.使用input 函数 接收一个四位年份 和 月份 判断这个月有多少天将结果输出到控制台(if的嵌套)
# 四位年份:非闰年和闰年
# 月份:
# 1,3,5,7,8,10,12 大月 31天
# 4,6,9,11 小月 30天
# 2 平月 (闰年29非闰年28)
# 第一步:输入年份和月份
year = int(input('请输入一个四位的年份:'))
month = int(input('请输入月份1-12'))
# 第二步:计算大月(不需要考虑闰年和非闰年)
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
print(f'{month}月是大月有31天')
# 第三步:计算小月(不需要考虑闰年和非闰年)
elif month == 4 or month == 6 or month == 9 or month == 11:
print(f'{month}月是小月有30天')
# 第四步计算2月(要考虑闰年和非闰年)(闰年29天非闰年28天)
# 除了上面的以外的情况(如果你输入的是1-12只剩下2月)
else: # 相当于你输入的是2月
# 第五步嵌套一个if语句来判断是否是闰年
# 闰年的判断条件能被4整除且不能被100整除或能被400整除
if (year % 4 == 0 and year % 100 != 0) or (year % 400 ==0):
print('你输入的月份是2月同时是闰年这个月有29天')
else:
print('你输入的月份是2月但不是闰年这个月有28天')