Python编程:从入门到实践 练习答案 Chapter05

20211029

5-1
car = 'suzuki'
print('Is car == "suzuki"? I predict True')
print(car == 'suzuki')

car = 'Jaguar'
print('Is car == "Jaguar"? I predict False')
print(car == 'jaguar')
...

5-2
car = 'suzuki'
print('Is car == "suzuki"? I predict True')
print(car == 'suzuki')

car = 'Jaguar'
print('Is car == "Jaguar"? I predict False')
print(car == 'jaguar')

print('Is car == "Jaguar"? I predict True')
print(car.lower == 'jaguar')

x, y, z = 1, 2, 1
print(x ==z, x != y, y > z, x < y, y >= z,x <= y)

print((x ==z) and (x != y))

print((x != z) or (x != y))

food = ['duck','chicken']
print('duck' in food)
print('fish' not in food)

5-3
alien_color = 'green'

if alien_color =='green':
    print('U got 5 points!')
    
alien_color = 'red'
if alien_color =='green':
    print('U got 5 points!')
    
5-4
alien_color = 'green'

if alien_color =='green':
    print('U got 5 points!')
else:
    print('U got 10 points!')
    
alien_color = 'red'
if alien_color =='green':
    print('U got 5 points!')
else:
    print('U got 10 points!')
5-5
alien_color = 'green'

if alien_color =='green':
    print('U got 5 points!')
elif alien_color =='yellow':
    print('U got 10 points!')
else:
    print('U got 15 points!')
    
alien_color = 'yellow'

if alien_color =='green':
    print('U got 5 points!')
elif alien_color =='yellow':
    print('U got 10 points!')
else:
    print('U got 15 points!')
    
alien_color = 'green'

if alien_color =='red':
    print('U got 5 points!')
elif alien_color =='yellow':
    print('U got 10 points!')
else:
    print('U got 15 points!')
    
  5-6
  age = 21
if age < 2:
    print("he's a baby")
elif age < 4:
    print("he's a toddler")
elif age < 13:
    print("he's a kid")
elif age < 20:
    print("he's a teenager")
elif age < 65:
    print("he's a adult")
else:
    print("he's a elder")  

5-7
favorite_fruit = ['lychee', 'Mandrin', 'longan', 'plum']
if 'plum' in favorite_fruit:
    print('U really like plums!')
if 'lychee' in favorite_fruit:
    print('U really like litchis!')
if 'Mandrin' in favorite_fruit:
    print('U really like Mandrin!')
if 'peach' in favorite_fruit:
    print('U really like peaches!')
if 'apple' in favorite_fruit:
    print('U really like apples!')

5-8
name = ['admin', 'I', 'L', 'W', 'X', 'Y']
for user in name:
    if user == 'admin':
        print('Hello admin, would U like to see a status report?')
    else:
        print('Hello ' + user + ', thank U for logging in again')

5-9
name = []
if name:
    for user in name:
        if user == 'admin':
            print('Hello admin, would U like to see a status report?')
        else:
            print('Hello ' + user + ', thank U for logging in again')
else:
    print('We need to finf some users!')

5-10/
current_users = ['S', 'I', 'L', 'W', 'X', 'Y']
new_users = ['s', 'Z', 'x', 'G', 'B']

current_users_lower = [cur_user.lower() for cur_user in current_users]

for user in new_users:
    if user.lower() in current_users_lower:
        print('name used, plz use another')
    else:
        print('name unused')
  
  5-11
  nums = list(range(1,10))
for num in nums:
    if num == 1:
        print('1st')
    elif num == 2:
        print('2nd')
    elif num == 3:
        print('3rd')
    else:
        print(str(num) + 'th')

5-12
...

5-13
...


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值