Github: GitHub -https://github.com/jackfrued/Python-100-Days
(骆昊)
Gitclone: https://gitee.com/viitii/Python-100-Days
10.13
Day03.分支结构
1.双分支结构 if …else…
username = input('请输入用户名: ')
password = input('请输入口令: ')
# 用户名是admin且密码是123456则身份验证成功否则身份验证失败
if username == 'admin' and password == '123456':
print('身份验证成功!')
else:
print('身份验证失败!')
报错注意:
(1)input(),print()函数,括号在函数名称之后
(2)if 与 else 之后要加冒号(英文),if+条件+: ,else+:
2.多分支结构 if…elif…elif…else…
"""
分段函数求值
3x - 5 (x > 1)
f(x) = x + 2 (-1 <= x <= 1)
5x + 3 (x < -1)
"""
x = float(input('x = '))
if x > 1:
y = 3 * x - 5
elif x >= -1</