《Python编程从入门到实践》练习题代码实现第五章

《Python编程从入门到实践》第二版 

练习题代码实现 第五章

#P69
#5-1条件测试
car='subaru'
print("Is car =='subaru'? I predict ture.")
print(car=='subaru')
print("\nIs car =='audi'? I predict false.")
print(car=='audi')

#5-2更多条件测试
a='abc'
b='abce'
c='Abc'
print("a==b? i predict false" )
print(a==b)
print("a==c? i predict false")
print(a==c)
print(a.lower()==c.lower())

#5-3外星人颜色
alien_color=['green','yellow','red']
if 'green' in alien_color:
    print("you got 5")

#5-4外星人颜色2
alien_color=['green','yellow','red']
if'green'in alien_color:
    print("u got 5")
else:
    print("u got 10")

#5-5外星人颜色3
alien_color='green'
if'green'in alien_color:
    print("u got 5")
elif'yellow'in alien_color:
    print("u got 10")
else:
    print("u got 15")

#5-6人生的不同阶段
age=24
if age<2:
    print("she is a baby")
elif age<4:
    print("she is a child")
elif age<13:
    print("she is a children")
elif age<20:
    print("she is a teenger")
elif age<65:
    print("she is a audlt")
else:
    print("she is a elder")

#5-7喜欢的水果
favorite_fruits=['banana','kiwifruit','watermelon']
if 'banana' in favorite_fruits:
    print("you really like bananas!")
if 'kiwifruit' in favorite_fruits:
    print("you really like kiwifruit!")
if 'watermelon' in favorite_fruits:
    print("you really like watermelon!")

#5-8以特殊方式跟管理员打招呼
user_names=['a','b','c','d','admin']
print(user_names)
print("欢迎登陆!")
for user_name in user_names:
    if user_name=='admin' :#'admin' in user_names:
        print("Hello admin,would you like to see a status report?")
    else:
        print("hello" ,user_name, "thank you for logging in again.")

#5-9处理没有用户的情形
user=[]
if user:
    print("welcome!")
else:
    print("we need to find some users!")

#5-10检查用户名
current_users = ['锦觅','旭凤','狐狸仙','润玉','admin']
new_users = ['旭凤','锦觅','邓论','杨紫','wo']
for value in new_users:
    value = value.lower()
    if value in current_users:
        print(value,'已被使用!请输入别的用户名:')
    else:
        print(value,'此用户名可以使用!')

#5-11序数
numbers = list(range(1,10))
print(numbers)
for value in numbers:
    value = str(value)
    if value == '1':
        print(value + 'st')
    elif value == '2':
        print(value + 'nd')
    elif value == '3':
        print(value + 'rd')
    else:
        print(value + 'th')

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值