《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