python编程,从入门到实践:第五章

python编程,从入门到实践:第五章

5-1 条件测试 :编写一系列条件测试;将每个测试以及你对其结果的预测和实际结果都打印出来。你编写的代码应类似于下面这样:

car = 'subaru' 
print("Is car == 'subaru'? I predict True.") 
print(car == 'subaru')     
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi'

详细研究实际结果,直到你明白了它为何为True 或False 。 创建至少10个测试,且其中结果分别为True 和False 的测试都至少有5个。

A="bus"
print("Is A == bus? I predict true")
print(A=='bus')
A="bus"
print("Is A == bike? I predict false")
print(A=='bike')

5-2 更多的条件测试 :你并非只能创建10个测试。如果你想尝试做更多的比较,可再编写一些测试,并将它们加入到conditional_tests.py中。对于下面列出的各种测 试,至少编写一个结果为True 和False 的测试。检查两个字符串相等和不等。 使用函数lower() 的测试。检查两个数字相等、不等、大于、小于、大于等于和小于等于。 使用关键字and 和or 的测试。测试特定的值是否包含在列表中。测试特定的值是否未包含在列表中。

A = 'Bus'
B = 'Bike'
C = 'Bus'
Age_01 = 19
Age_02 = 20
# 检查两个字符串相等或者不等
print(A==B)
print(A==C)
# 使用函数lower测试
print(A.lower())
# 检查两个数字相等、不等、大于、小于、大于等于和小于等于
print(Age_01==Age_02)
print(Age_01!=Age_02)
print(Age_01>Age_02)
print(Age_01<Age_02)
print(Age_01>=Age_02)
print(Age_01<=Age_02)
# 使用关键字 and or 测试
print(Age_02 > Age_01 and A == B )
print(Age_02 > Age_01 or A == B )
# 测试特定的值是否在列表中
Num = list(range(1,11))
if 1 in Num:
    print("在里面")
else:
    print("不在里面")
    # 测试特定的值是否未包含在列表中
Num = list(range(1,11))
if 1 not  in Num:
    print("不包含")
else:
    print("包含")

5-3 外星人颜色#1 :假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为’green’ 、‘yellow’ 或’red’ 。 编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。

Alien_color = 'green'
# Alien_color = 'yellow'
# Alien_color = 'red'
if Alien_color == 'green':
    print("You got 5 points")

5-4 外星人颜色#2 :像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。 如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。 如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。 编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执行else 代码块。

# Alien_color = 'green'
Alien_color = 'yellow'
# Alien_color = 'red'
if Alien_color == 'green':
    print("You got 5 points")
else:
    print("You got 10 points")

5-5 外星人颜色#3 :将练习5-4中的if-else 结构改为if-elif-else 结构。 如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点。 如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点。 如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。

# Alien_color = 'green'
Alien_color = 'yellow'
# Alien_color = 'red'
if Alien_color == 'green':
    print("You got 5 points")
elif Alien_color == 'yellow':
    print("You got 10 points")
elif Alien_color == 'red':
    print("You got 10 points")

编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。
5-6人生的不同阶段 :设置变量age 的值,再编写一个if-elif-else 结构,根据age 的值判断处于人生的哪个阶段。 如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。 如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步。 如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。 如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。 如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。 如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人。

while(1):
    print("In put your age.")
    Age = input()
    Age = int(Age)
    if Age < 2 :
        print("He is a baby\n")
    elif Age >= 2 and Age < 4:
        print("He is a toddler\n")
    elif Age >= 4 and Age < 13:
        print("He is a boy\n")
    elif Age >= 13 and Age <20:
        print("He is a teenagers\n")
    elif Age >= 20 and Age < 65:
        print("He is a man\n")
    elif Age > 65:
        print("He is a old man\n")

5-7 喜欢的水果 :创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if 语句,检查列表中是否包含特定的水果。
将该列表命名为favorite_fruits ,并在其中包含三种水果。
编写5条if 语句,每条都检查某种水果是否包含在列表中,如果包含在列表中,就打印一条消息,如“You really like bananas!”。

Favorite_fruit = ['apple','banana','peach']
if 'apple' in Favorite_fruit:
    print("You really like apple!")
if 'banana' in Favorite_fruit:
    print("You really like banana!")
if 'peach' in Favorite_fruit:
    print("You really like peach!")
if 'pear' in Favorite_fruit:
    print("You really like pear!")
if 'orange' in Favorite_fruit:
    print("You really like orange!")

5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为’admin’ 。想象你要编写代码,在每位用户登录网站后都打印一条问 候消息。遍历用户名列表,并向每位用户打印一条问候消息。
如果用户名为’admin’ ,就打印一条特殊的问候消息,如“Hello admin, would you like to see a status report?”。
否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。

while(1):
    User_lists = ['a','b','c','d','admin']
    User = input()
##  for User_list in User_lists:
    if User in User_lists:
        if User == 'admin':
            print("Hello admin ,would you like to see a status report")
        else:
            print("Hello " + User + " , think you for logging in again")
    else:
        print("Sorry ," + User + "illegal user" )

5-9 处理没有用户的情形 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。
如果为空,就打印消息“We need to find some users!”。
删除列表中的所有用户名,确定将打印正确的消息。

while(1):
##  User_lists = ['a','b','c','d','admin']
    User_lists = []
    User = input()
##  for User_list in User_lists:
    if User_lists:
        if User in User_lists:
            if User == 'admin':
                print("Hello admin ,would you like to see a status report")
            else:
                print("Hello " + User + " , think you for logging in again")
        else:
            print("Sorry ," + User + "illegal user" )
    else :
        print("We need to find some user.")

5-10 检查用户名 检查用户名 :按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
创建一个至少包含5个用户名的列表,并将其命名为current_users 。
再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。
遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指 出这个用户名未被使用。
确保比较时不区分大消息;换句话说,如果用户名’John’ 已被使用,应拒绝用户名’JOHN’ 。

Current_userss = ['a','B','C','d','e']
Current_users = []
for Current_user in Current_userss:
    Current_users.append(Current_user.lower())
New_users = ['A','f','i','g','k']
if New_users:
    for New_user in New_users:
        if New_user.lower() in Current_users:
            print(New_user + " has been used.")
        else:
            print(New_user + " can be used")

5-11 序数 :序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。
在一个列表中存储数字1~9。
遍历这个列表。
在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序 数都独占一行。

List = ['1','2','3','4','5','6','7','8','9']
for Num in List:
    if Num == '1':
        print("1st")
    elif Num == '2':
        print("2nd")
    elif Num == '3':
        print("3rd")
    else:
        print(Num +'th')

5-12
已审核,未发现异常
5-13
没啥想法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值