《Python编程从入门到实践》习题03

《Python编程从入门到实践》——动手试一试

if语句题目
书本70页

5-1

competitors = ['theshy','rookie','jacklove','feaker']
competitor = 'theshy'
print(competitor == 'theshy')
print(competitor == 'rookie')
print(competitors[0] == 'Theshy')
print(competitors[0].title() == 'Theshy')
print(competitors[1] == 'Theshy')
True
False
False
True
False

5-2

competitors = ['theshy','rookie','jacklove','feaker'.title()]
competitor = 'theshy'
print(competitors[0] != competitor)
print(competitors[-1].lower() == 'feaker')
print(9 == 7)
print(9 >= 7 and competitors[0] != 'theshy')
print(9 >= 7 or competitors[0] != 'theshy')
if 'theshy' in competitors:
    print('yes')
if 'THESHY' in competitor:
    print('yes')
else:
    print('no')
False
True
False
False
True
yes
no

书本75页
#外星人颜色

aline_color = 'red'
if aline_color == 'green':
    print('Players get 5 points.')
elif aline_color == 'yellow':
    print('Players get 10 points.')
elif aline_color == 'red':
    print('Players get 15 points.')
#
Players get 15 points.

#人生不同阶段

age = 18
if age <= 2:
    print("He's a baby.")
elif age >= 2 and age < 4:
    print("He's learning how to walk.")
elif age >= 4 and age < 13:
    print("He's a kid.")
elif age >= 13 and age < 20:
    print("He's a younge.")
else:
    print("I don't want to type.")
#
He's a younge.

#喜欢的选手

competitors = ['theshy','rookie','jacklove','feaker'.title()]
if 'theshy' in competitors:
    print('You really like theshy')
if 'rookie' in competitors:
    print('You really like rookie')
if 'jacklove' in competitors:
    print('You really like jacklove')
if 'feaker' in competitors:
    print('You really like feaker')
#
You really like theshy
You really like rookie
You really like jacklove

书本79页
#打招呼

competitors = ['theshy','rookie','jacklove','feaker','Ning']
for competitor in competitors:
    print('Hello,' + competitor)
    if competitor == 'theshy':
        print('Hello,' + competitor.title() + 'I am you fan!!!')
#
Hello,theshy
Hello,TheshyI am you fan!!!
Hello,rookie
Hello,jacklove
Hello,feaker
Hello,Ning

#特殊情况

competitors = ['theshy','rookie','jacklove','feaker','Ning']
if competitors == []:
    print('We need comptitors!')
else:
    competitors.clear()
    if competitors == []:
        print('We need comptitors!')
#
We need comptitors!

#检查用户名

competitors = ['theshy','rookie','jacklove','feaker','Ning']
competitors_1 = ['theshy','rookie','369','zoom','Ning']
for competitor in competitors_1:
    if competitor in competitors:
        print('The name,'+ competitor +' has been used.')
    else:
        print('Hello,' + competitor + '!')
#
The name,theshy has been used.
The name,rookie has been used.
Hello,369!
Hello,zoom!
The name,Ning has been used.

#序数

num = [x for x in range(1,12)]
for x in num:
    if x == 1:
        print(str(x) + 'st')
    elif x == 2:
        print(str(x) + 'st')
    elif x == 3:
        print(str(x) + 'st')
    else:
        print(str(x) + 'th')
#
1st
2st
3st
4th
5th
6th
7th
8th
9th
10th
11th
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值