if条件语句:满足条件去执行语句。
>>> numbers = list(range(0,11))
>>> if 3 in numbers: #注意冒号
... print('hello') # 注意要缩进 满足条件打印 hello
...
hello
#检验两个值是否相等 == 不相等!=
#检查是否相等时不区分大小写
#检查多个条件 可以加上逻辑运算符 and or
>>> car = 'Audi'
>>> car.lower() == 'audi'
True
#网站常采用类似的格式让用户创建符合格式的用户名
#检查成员资格 in not in
if语句:
1.简单的if语句
>>> if x: # 只要x是非零数值,非空字符串,非空list等就会执行
... print(True)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
2.if-else
常用于两种操作之一的情形
>>> if 5 > 6:
... print('haha')
... else:
... print('wahaha')
...
wahaha
>>>
3.if-elif-elif....-else
依次检查每个条件测试,直到遇到通过的条件测试,通过后,执行相应的代码,并跳过余下测试。
>>> # 根据年龄收费 4岁以下免费 , 4-18岁收5元,18岁以上10元,65岁以上免费
>>> age = int(input("请输入你的年龄: "))
请输入你的年龄: 23
>>> if age < 4:
... print("You should pay 0 yuan.")
... elif age < 18:
... print("You should pay 5 yuan.")
... elif age < 65:
... print("You should pay 10 yuan.")
... else:
... print("You should pay 0 yuan.")
...
You should pay 10 yuan.
>>># 尽量让if语句简单 可以提高效率,并且容易修改
>>> age = int(input("请输入你的年龄: "))
请输入你的年龄: 23
>>> if age < 4:
... price = 0
... elif age < 18:
... price = 5
... elif age < 65:
... price = 10
... else: # 知道最终要测试条件,可以用elif代替else
... price = 0
...
>>>print("You should pay " + str(price) + " yuan."
You should pay 10 yuan.
测试多个条件用一系列if语句;
执行一个代码块,可以用if,if-else,if-elif-elif..-else
运行多个代码块,可以使用一系列独立的if语句。
习题:
第一题:以特殊方式跟管理员打招呼:
创建一个至少包含 5 个用户名的列表,且其中一个用户名为'admin'。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。
如果用户名为'admin',就打印一条特殊的问候消息,如"Hello admin, would you like to see a status report?"。
否则,打印一条普通的问候消息 ,如"Hello Eric, thank you for logging in again"。
第二题:处理没有用户的情形:
在为完成第一题编写的程序中,添加一条 if 语句,检查用户名列表是否为空。
如果为空,就打印消息“We need to find some users!”。
删除列表中的所有用户名,确定将打印正确的消息。
>>> #第一题
... usernames = ['eric', 'willie', 'admin', 'erin', 'ever']
>>>
>>> for username in usernames:
... if username == 'admin':
... print("Hello admin, would you like to see a status report?")
... else:
... print("Hello " + username + ", thank you for logging in again!")
...
Hello eric, thank you for logging in again!
Hello willie, thank you for logging in again!
Hello admin, would you like to see a status report?
Hello erin, thank you for logging in again!
Hello ever, thank you for logging in again!
>>>第二题
>>> usernames = []
>>>
>>> if usernames: # 判断列表是否为空。
... for username in usernames:
... if username == 'admin':
... print("Hello admin, would you like to see a status report?")
... else:
... print("Hello " + username + ", thank you for logging in again!")
... else:
... print("We need to find some users!")
...
We need to find some users!
>>>
第三题:
检查用户名:按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
创建一个至少包含 5 个用户名的列表,并将其命名为 current_users。
再创建一个包含 5 个用户名的列表,将其命名为 new_users,并确保其中有一两个用户名也包含在列表 current_users 中。
遍历列表 new_users,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用。
确保比较时不区分大小写;换句话说,如果用户名'John'已被使用,应拒绝用户名'JOHN'
>>> current_users = ['eric', 'willie', 'admin', 'erin', 'Ever']
>>> new_users = ['sarah', 'Willie', 'PHIL', 'ever', 'Iona']
>>>
>>> current_users_lower = [user.lower() for user in current_users]
>>>
>>> for new_user in new_users:
... if new_user.lower() in current_users_lower:
... print("Sorry " + new_user + ", that name is taken.")
... else:
... print("Great, " + new_user + " is still available.")
...
Great, sarah is still available.
Sorry Willie, that name is taken.
Great, PHIL is still available.
Sorry ever, that name is taken.
Great, Iona is still available.