if综合练习

1.

1).从控制台输入你要出的拳 ---石头(1)/剪刀(2)/布(3)
2).电脑随即出拳
3).比较胜负

import  random

#1.从控制台输入要出的拳
player = int(input('请输入您要出的拳:石头1/剪刀2/布3:'))

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

#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('玩家失败~')

2.

判断闰年
用户输入年份,判断是否为闰年?
- 能被400整除的是闰年,能被4整除但是不能被100整除的是闰年

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

if (year % 4 == 0 and year % 100 != 0) \
        or (year % 400 == 0):
    print('%s是闰年' %year)
else:
    print('%s不是闰年' %year)

3.

输入年、月,输出本月有多少天
2004 2
29天
2010 4
30天

year = int(input("year"))
month = int(input("month"))
c=1
if year % 4 == 0 and year % 100 != 0:
    c=1
elif year % 400 == 0:
    c=1
else:
    c=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 has 31 days" %(year,month))
elif month==4 or month==6 or month==11 or month==9:
    print("%d-%d has 30 days" %(year,month))
elif c==1:
    print("%d-%d has 29 days" %(year,month))
else:
    print("%d-%d has 28 days" % (year, month))

4.

用if判断输入的值是否为空,如果为空,报错Error

c=str(input("your type"))
if c=="":
    print("Error")
else:
    print("yes")

5.

用户输入月份,打印该月份所属的季节

3,4,5春季  6,7,8夏季  9,10,11秋季 12 1 2冬季

month = int(input("month"))
if 1<=month<=2 or month==12 :
    print("winter")
elif 3<=month<=5:
    print("spring")
elif 6<=month<=8:
    print("summer")
elif 9<=month<=11:
    print("autumn")
else:
    print("error")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值