Python---1.2 if和else语句

第二节学习一下if和else语句如何使用吧


(1)最简单的if-else判断语句:
if a < b:
   print("yes")
else:
   print("no")
(2)要创建一条空语句可以用pass;

if a < b:
    pass
else:
    print("no")
#(3)使用or,and,not关键词可以建立布尔类型的表达式:

if product == "game" and type== "pirate memory" \
                     and not (age < 4  or  age > 8):
    print ("I will take it!")
#要处理多个测试用例的话可以选用elif

小练习:
(1)从控制台输入你要出的拳  石头(1)/剪刀(2)/布(3)
电脑随机出拳,比较胜负;
"""

import random
player = int(input('请输入你要出的拳:石头(1)/剪刀(2)/布(3):'))
computer = random.randint(1,3)
print("玩家:%d,电脑:%d" %(player,computer))

if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
    print("Congratulation.player!!!")
elif player == computer:
    print("平局")
else:
    print("Fail")
(2)判断闰年
year能被4整除但是不能被100整除,或者year能被400整除就是闰年;
除号:%
不等于:!=
year = int(input('请输入要测试的年份:'))
if ((year %4 == 0) and (year %100 != 0) or (year %400 == 0)):
    print("您输入的年份%d为闰年!" %(year))
else:
    print("您输入的年份不是闰年!")
(3)猜数字游戏:
随机输入一个数字,电脑也会随机出现一个数字,如果大了,电脑会提示大了,小了会提示小了
直到两个的数字相同;
"""

import random
player = int(input('请输入一个数字:'))
computer = random.randint(0,1000)
if (player>999):
    print('请输入一个小于三位数的数字')
    exit()
elif (player > computer):
    print("大了!")

elif (player < computer):
    print('小了!')
else:
    print("Boom!")
print("电脑给出的数字:%d\n你给出的数字:%d" %(computer,player))

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值