栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

《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','yellow','red']
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,11))
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')

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号