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
choose−random−number
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.