if&else语句的基础练习们

if else语句的基础练习们

用户身份验证

#用户身份验证
username = input('Please enter your name:')
password = input('Please enter your password:')
if username=='admin' and password=='123':
    print('Welcome!')
else:
    print('Forget the password?')
Please enter your name:zilu
Please enter your password:123
Forget the password?
>>> 
Please enter your name:admin
Please enter your password:123
Welcome!

==================================
分段函数

##if else practice
"""
    3x-5,   x>1
y = x+2 , -1<=x<=1
    5x+3,   x<-1
"""

x = float(input('Please enter x = '))
if x > 1:
    y = 3*x-5
elif x>=-1:
    y = x+2
else:
    y = 5*x+3
print('{:.3f}'.format(y))

Please enter x = 2
1.000

================================
c h o o s e − r a n d o m − n u m b e r choose-random-number chooserandomnumber

face = int(input('Please enter your random number: '))
if face==1:
    result = 'sing a song'
elif face==2:
    result = 'dance'
elif face==3:
    result = 'drink the cola'
elif face==4:
    result = 'drink the wine'
elif face==5:
    result = 'tell a joke'
else:
    result = 'tell a fairy tale'
print('you chose to '+result)

Please enter your random number: 7
you chose to tell a fairy tale

=============================
编码实现等级评定 70-80 rankC 左开右闭

#####0128 assignment 01

score = int(input('Please enter your score: '))
if score>90 and score<=100:
    result = 'A'
elif score>80 and score<=90:
    result = 'B'
elif score>70 and score<=80:
    result = 'C'
elif score>=0 and score<=70:
    result = 'D'
else:
    result = 'error'
print('Your rank is: '+result)
Please enter your score: 92
Your rank is: A

===============================
编码实现判断输入的三个数 可否构成三角形

##### 0128 assignment 02 triangle

a = float(input('Please enter a = '))
b = float(input('Please enter b = '))
c = float(input('Please enter c = '))
if c>=a and c>=b and a+b>c:
    print('{} {} and {} can form a triangle.'.format(a,b,c))
elif b>=a and b>=c and a+c>b:
    print('{} {} and {} can form a triangle.'.format(a,b,c))
elif a>=b and a>=c and b+c>a:
    print('{} {} and {} can form a triangle.'.format(a,b,c))
else:
    print('{} {} and {} can\'t form a triangle.'.format(a,b,c))
Please enter a = 22
Please enter b = 30
Please enter c = 50
22.0 30.0 and 50.0 can form a triangle.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值