动手试一试
5.1条件测试
car = 'subaru';
print("Is car == 'subaru'? I predict True.");
print(car == 'subaru');
print("\nIs car == 'audi'? I predict False.");
print(car != 'audi');
5.3外星人颜色#1
alien_color=['blue','green','red'];
if 'green' in alien_color:
print("5");
5.4外星人颜色#2
alien_color=['blue','green1','red'];
if 'green' in alien_color:
print("5");
else:
print("10");
5.6人生的不同阶段
age=15;
if age < 2:
print("婴儿");
elif age < 4:
print("蹒跚学步");
elif age < 13:
print("儿童");
elif age < 20:
print("青少年");
elif age < 65:
print("中年人");
elif age >= 65:
print("老年人");
5.8以特殊方式向管理员打招呼
a=["001","002","003","004","admin"];
for cell in a:
if cell == "admin":
print("hello admin, would you like to see a status report?");
else:
print("hello Eric, thank you for logging in again");
5.9处理没有用户的情况
a=["001","002","003","004","admin"];
b=[];
if b:#if a:
for cell in a:
if cell == "admin":
print("hello admin, would you like to see a status report?");
else:
print("hello Eric, thank you for logging in again");
else:
print("we need to find some users");
5.10检查用户名
name=["a","b","c","d","e"];
name1=["A","C","f","g","h"];
name2=[];
for cell in name:
name2.append(cell.lower());
print(name2);
for cell in name1:
if cell.lower() in name2:
print("Yes");
else:
print("No");
5.11序数
a=list(range(1,11));
for cell in a:
if cell == 1:
print(str(cell)+"st");
elif cell == 2:
print(str(cell)+"nd");
elif cell == 3:
print(str(cell)+"rd");
else :
print(str(cell)+"th");