# 从零开始学Python—第五课：分支结构

if语句的使用

``````
""
"

Version: 0.1

Author: 骆昊

"
""
input(
'请输入用户名: ')
input(
'请输入口令: ')
123456则身份验证成功否则身份验证失败

'123456':

print(
'身份验证成功!')

else:

print(
'身份验证失败!')``````

``````
"""

Version: 0.1

Author: 骆昊

"""
x = float(input(
'x = '))

if x >
1:
y =
3 * x -
5

elif x >=
-1:
y = x +
2

else:
y =
5 * x +
3

print(
f'f(
{x}) =
{y}')``````

``````
"""

Version: 0.1

Author: 骆昊

"""
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'f(
{x}) =
{y}')``````

``````
"""

Version: 0.1

Author: 骆昊

"""
value = float(input(
'请输入长度: '))
unit = input(
'请输入单位: ')

if unit ==
'in'
or unit ==
'英寸':
print(
'%f英寸 = %f厘米' % (value, value *
2.54))

elif unit ==
'cm'
or unit ==
'厘米':
print(
'%f厘米 = %f英寸' % (value, value /
2.54))

else:
print(
'请输入有效的单位')``````

``````
"""

Version: 0.1

Author: 骆昊

"""
score = float(input(
'请输入成绩: '))

if score >=
90:
'A'

elif score >=
80:
'B'

elif score >=
70:
'C'

elif score >=
60:
'D'

else:
'E'
print(

``````
"""

Version: 0.1

Author: 骆昊

"""
a = float(input(
'a = '))
b = float(input(
'b = '))
c = float(input(
'c = '))

if a + b > c
and a + c > b
and b + c > a:
peri = a + b + c
print(
f'周长:
{peri}')
half = peri /
2
area = (half * (half - a) * (half - b) * (half - c)) **
0.5
print(
f'面积:
{area}')

else:
print(
'不能构成三角形')``````

• 博文量
995
• 访问量
584166