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

#-*-coding:utf-8 -*-
#5-1

superhero = 'spider-man'
print ( 'is superhero == spider-man?' , 'i predict true' )
print (superhero == 'spider-man' )

print ( 'is superhero == batman?' , 'i predict false' )
print (superhero == 'batman' )

food = 'apple'
print ( 'is food == apple?' , 'i predict true' )
print (food == 'apple' )

print ( 'is food == banana ?' , 'i predict false' )
print (food == 'banana' )

game = 'gtav'
print ( 'is game == gtav?' , 'i predict true' )
print (game == 'gtav' )

print ( 'is game == sleeping dog?' , 'i predict false' )
print (game == 'sleeping dog' )

#5-2
print ( 'pizza' == 'pizza' )
print ( 'pizza' == 'banana' )

print ( 'APPLE' .lower() == 'apple' )
print ( 'BANANA' .lower() == 'apple' )

print ( 5 == 5 )
print ( 4 == 3 )
print ( 100 > 9 )
print ( 100 > 200 )
print ( 100 >= 100 )
print ( 34234 >= 234123412 )
print ( 7 < 6 )
print ( 7 < 10 )
print ( 7 <= 7 )
print ( 7 <= 6 )
print ( 5 != 5 )
print ( 6 != 10 )

print ( 100 > 200 and 100 < 200 )
print ( 513 >= 223 and 'apple' == 'Apple' .lower())

print ( 345 == 566 or '3' == '3' )
print ( 23123 <= 23123456 or 'l' in 'feet' )

print ( 'aaaa' in [ 'aaaa' , 'bbbb' , 'cccc' ])
print ( 'abcd' in [ 'piece' , 'feet' ])

print ( 456 not in [ 234 , 345 , 456 ])
print ( 666 not in [ 111 , 222 , 333 ])

#5-3
alien_color = [ 'green' , 'yellow' , 'red' ]
if ( 'green' is alien_color[ 0 ]):
print ( 'you get 5 scores !' )

if ( 'blue' is alien_color[ 2 ]):
pass
#5-4
if ( 'green' is alien_color[ 0 ]):
print ( 'you will get 5 scores cause you shoot him' )
else :
print ( 'you get 10 scores !' )

if ( 'green' is alien_color[ 2 ]):
print ( 'you will get 5 scores cause you shoot him' )
else :
print ( 'you get 10 scores !' )

#5-5

#5-6
def jud_age ( age ):
if age < 2 :
print ( 'baby' )
elif age >= 2 and age < 4 :
print ( 'learn how to walk' )
elif age >= 4 and age < 13 :
print ( 'child' )
elif age >= 13 and age < 20 :
print ( 'teenager' )
elif age >= 20 and age < 65 :
print ( 'adult' )
else :
print ( 'old man' )
#5-7
favorite_fruits = [ 'pizza' , 'beijing_duck' , 'banana' ]
if 'apple' not in favorite_fruits:
print ( 'sorry!' )
else :
print ( 'you really like banana' )
if 'pizza' not in favorite_fruits:
print ( 'sorry!' )
else :
print ( 'you really like pizza' )
if 'shit' not in favorite_fruits:
print ( 'sorry!' )
else :
print ( 'you really like shit' )
if 'bread' not in favorite_fruits:
print ( 'sorry!' )
else :
print ( 'you really like bread' )
if 'beijing_duck' not in favorite_fruits:
print ( 'sorry!' )
else :
print ( 'you really like beijing_duck' )

#5-8
user = [ 'admin' , 'xiaojz' , 'yangxh' , 'yangxy' , 'lr' ]
for i in user:
if i == 'admin' :
print ( 'hello admin , would you like to see a status report?' )
else :
print ( 'hello' ,i, 'thank you for logging in again' )
#5-9
if user:
for i in user:
if i == 'admin' :
print ( 'hello admin , would you like to see a status report?' )
else :
print ( 'hello' ,i, 'thank you for logging in again' )
else :
print ( 'We need to find some users!' )

#5-10
def L ( s ):
return str (s).lower()
current_users = [ 'John' , 'jack' , 'lee' , 'hugh' , 'kehan' ]
new_users = [ 'JOHN' , 'Jack' , 'rick' , 'mo' , 'Hugh' ]
for a in new_users:
if a.lower() in map (L,current_users):
print ( 'used!' )
else :
print ( 'not used!' )


#5-11
number = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
for i in number:
if i == 1 :
print ( str (i) + 'st' )
elif i == 2 :
print ( str (i) + 'nd' )
elif i == 3 :
print ( str (i) + 'rd' )
else :
print ( str (i) + 'th' )

#5-12
#已经正确设置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值