Python中的if语句综合练习

判断闰年

要求:

"""
判断闰年?
用户输入年份year, 判断是否为闰年?
能被4整除但不能被100整除的  或者 能被400整除 那么就是闰年
"""

代码如下:

year = int(input('请输入年份:'))
if year % 4 == 0 and year % 100 != 0 or year %400 == 0:
    print('%d年是闰年' %year)
else:
    print('%d年是平年' %year)

执行结果:
在这里插入图片描述

判断某年某月有多少天

要求:

"""
 1. 输入年、月,输出本月有多少天。合理选择分支语句完成设
计任务。
        输入样例1:2004 2
        输出结果1:本月29天
        输入样例2:2010 4
        输出结果2:本月30天
"""

我写的:

year = int(input('请输入年份:'))
month = int(input('请输入月份:'))
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
    if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
        print('%d年%d月有31天' %(year,month))
    elif month == 2:
        print('%d年%d月有29天' %(year,month))
    else:
        print('%d年%d月有30天' %(year,month))
else:
    if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
        print('%d年%d月有31天' %(year,month))
    elif month == 2:
        print('%d年%d月有28天' %(year,month))
    else:
        print('%d年%d月有30天' %(year,month))

执行结果:
在这里插入图片描述
老师写的:

year = int(input('Year:'))
month = int(input('Month:'))

if (month == 1 or month == 3 or month == 5 or month ==7 or month == 8 or month == 10 or month == 12):
    print('31天')
elif (month == 4 or month == 6 or month == 9 or month == 11):
    print('30天')
elif month == 2 and ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):
    print('29天')
else:
    print('28天')

执行结果:
在这里插入图片描述
在shell中通过命令cal + 年份 可以查看某一年的日历
在这里插入图片描述

石头剪刀布

要求:

"""
1.从控制台输入要出的拳 --石头1 剪刀2 布3
2.电脑随机出拳 
3.比较胜负

"""

我写的:

import random
computer = random.randint(1,3)

userPrint = int(input('请出拳: 石头(1)剪刀(2)布(3)'))

if computer == 1 and userPrint == 2 or computer == 2 and userPrint == 3 or computer == 3 and userPrint == 1:
    print('computer win')
elif computer == userPrint:
    print('平局')
else:
    print('user win')

print(computer)

执行结果:
在这里插入图片描述
老师写的:

import random
#1.用户出拳
player = int(input('请输入您要出的拳:石头(1)剪刀(2)布(3):'))

#2.电脑出拳
computer = random.randint(1,3)

#3.比较胜负
if ((player == 1 and computer == 2)
    or (player == 2 and computer == 3)
    or (player == 3 and computer == 1)):
    print('玩家赢')
elif player == computer:
    print('平局')
else:
    print('玩家输')

执行结果:
在这里插入图片描述

判断学生成绩的等级

85以上 优秀
70~85 良好
60~70 及格
60以下 不及格

代码如下:

score = float(input("请输入学生成绩:"))

if score >= 60:
    if score >= 70:
        if score >= 85:
            print('优秀')
        else:
            print('良好')
    else:
        print('及格')
else:
    print('不及格')

执行结果:
在这里插入图片描述

输入三个数,找出其中最大的

我写的:

a = float(input('请输入第一个数:'))
b = float(input('请输入第二个数:'))
c = float(input('请输入第三个数:'))
max=max(a,b,c)
print(max)

执行结果:
在这里插入图片描述
思路如下:
输入三个数a,b,c,假定第一个数a是最大数,将第一个数的值送给max,然后将max分别和b,c比较,两次比较后,max的值即为a,b,c中的最大值。
代码如下:

a = int(input('a=:'))
b = int(input('b=:'))
c = int(input('c=:'))
max = a
if c>b:
    if c>a:
        max=c
else:
    if b>a:
        max=b
print('最大数是',max)

执行结果:
在这里插入图片描述

生成3个2位随机整数,输出其中最大的数

程序如下:

import random
x=random.randint(10,99)
y=random.randint(10,99)
z=random.randint(10,99)
max=x if x>y else y
max=max if max>z else z
print("x={0},y={1},z={2}".format(x,y,z))
print("max=",max)

执行结果:
在这里插入图片描述

水仙花数

输入一个整数,判断是否为水仙花数,是指三位整数,各位数字的立方和等于该数本身。例如153。
分析:先分别求出3位整数的个位,十位,百位数字,再根据条件判断是否为水仙花数。
代码如下:

a = int(input('请输入一个三位整数100~999:'))
gewei = a % 10
shiwei = (a //10) %10
baiwei = a // 100

if gewei * gewei * gewei + shiwei * shiwei * shiwei + baiwei * baiwei * baiwei == a:
    print('%d是水仙花数' %a)
else:
    print('%d不是水仙花数' %a)

执行结果:
在这里插入图片描述

输入一个时间

输入一个时间(小时:分钟:秒),输出该时间经过5分30秒后的时间
代码如下:

hour = int(input('请输入小时:'))
minute = int(input('请输入分钟:'))
second = int(input('请输入秒:'))

second += 30
minute += 5

if second >= 60:
    second = second-60
    minute += 1

if minute >= 60:
    minute = minute-60
    hour += 1

if hour == 24:
    hour = 0
print('{0:d}:{1:d}:{2:d}'.format(hour,minute,second))

执行结果:
在这里插入图片描述

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值