Day3 分支结构

username = input('请输入用户名:')
password = input('请输入口令:')
if username == 'admin' and password == '123456':
    print('身份验证成功!')
else:
    print('身份验证失败!')

if语句不用(),必须要加 :

x = float(input('x='))
if(x>1):
    print('f(x)=%.2f' % (3*x-5))
elif(x<-1):
    print('f(x)=%.2f' % (5*x+3))
else:
    print('f(x)=%.2f' % (x+2))



x=float(input('x='))
if x>1:
    y = 3 * x - 5
elif x<-1:
    y = 5 * x +3
else:
    y = x + 2
print('f(%.2f)=%.2f' % (x,y))



x=float(input('x='))
if x>1:
    y = 3 * x - 5
else:
    if x>=-1:
        y=x+2
    else:
        y=5*x+3
print('f(%.2f)=%.2f' % (x,y))

value = float(input('请输入长度:'))
unit = input('请输入单位:(英寸/厘米):')

if unit=='英寸':
    print('%f英寸 = %f厘米' % (value,value*2.54))
elif unit=='厘米':
    print('%f厘米 = %f英寸' % (value, value / 2.54))
else:
    print('请输入有效的单位。')

当输出的变量是同一个,要想到用变量存储起来。这样就只用一条输出语句,而且语句更清晰

score = float(input('请输入分数:'))
if score >= 90:
    print('A')
elif (score >=80 and score < 90):
    print('B')
elif (score >=70 and score < 80):
    print('C')
elif (score >=60 and score < 70):
    print('D')
else:
    print('E')


score = float(input('请输入您的成绩:'))
if score>=90:
    grade='A'
elif score>=80:
    grade='B'
elif score>=70:
    grade='C'
elif score>=60:
    grade='D'
else:
    grade='E'
print('您成绩对应的等级是:',grade)

image.png

image.png
a = float(input('a='))
b = float(input('b='))
c = float(input('c='))

if a+b>c and a+c>b and b+c>a :
    p = a+b+c
    t=p/2
    area = (t*(t-a)*(t-b)*(t-c))**0.5
    print('该三角形的周长为%.2f'% p)
    print('该三角形的面积为%.2f'% area)
else:
    print('不能构成三角形')
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容