python编程从入门到实践-第五章习题

动手试一试

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");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值