《Python编程:从入门到实践》课后习题-第5章

5-1 条件测试

#5-1 条件测试

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

name = 'Susan'
print("\nHer name == 'Susan'? I predict True.")
print(name == 'Susan')
print("Her name == 'Rebecca'? I predict False.")
print(name == 'Rebecca')
name = 'Jenny'
print("Her name == 'Ariana'? I predict False.")
print(name == 'Ariana')
print("Her name == 'Jenny'? I predict False.")
print(name == 'Jenny')
name = 'Rihana'
print("Her name == 'Rihana'? I predict True.")
print(name == 'Rihana')

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

5-2 更多的条件测试

#5-2 更多的条件测试

print('forbidden' == 'behave')
print('forbidden' == 'forbidden')
name = 'ROSE'
print('ROSE' == name.lower())

age = 18
print(age == 8)
print(age != 8)
print(age > 8)
print(age < 8)
print(age >= 8)
print(age <= 8)
print(age == 8 and age != 8)
print(age == 8 or age != 8)

lists = ['onion','carrot','vegetables']
print('carrot' in lists)
print('beef' in lists)

5-3 外星人颜色#1

#5-3 外星人颜色#1

alien_color = 'green'
if alien_color == 'green':
    print "玩家获得5个点。"
alien_color = 'yellow'
if alien_color == 'green':
    print "玩家获得5个点。"

5-4 外星人颜色#2

#5-4 外星人颜色#2

alien_color = 'yellow'
if alien_color == 'green':
    print"玩家因射杀该外星人获得了5个点"
else:
    print"玩家获得了10个点"

alien_color = 'green'
if alien_color == 'green':
    print"玩家因射杀该外星人获得了5个点"
else:
    print"玩家获得了10个点"

5-5 外星人颜色#3

#5-5 外星人颜色#3

alien_color = 'yellow'
if alien_color == 'green':
    print"玩家获得了5个点"
elif alien_color == 'yellow':
    print"玩家获得了10个点"
else:
    print"玩家获得了15个点"

alien_color = 'green'
if alien_color == 'green':
    print"玩家获得了5个点"
elif alien_color == 'yellow':
    print"玩家获得了10个点"
else:
    print"玩家获得了15个点"

alien_color = 'red'
if alien_color == 'green':
    print"玩家获得了5个点"
elif alien_color == 'yellow':
    print"玩家获得了10个点"
else:
    print"玩家获得了15个点"

5-6 人生的不同阶段

#5-6 人生的不同阶段

age = 66
if age < 2:
    print"他是婴儿"
elif age <4:
    print"他正蹒跚学步"
elif age <13:
    print"他是儿童"
elif age <20:
    print"他是青少年"
elif age <65:
    print"他是成年人"
else:
    print"他是老年人"

5-7 喜欢的水果

#5-7 喜欢的水果

favorite_fruits = ['orange','watermelon','banana']
if 'orange' in favorite_fruits:
    print"You really like bananas!"
if 'apple' in favorite_fruits:
    print"You really like bananas!"
if 'grape' in favorite_fruits:
    print"You really like bananas!"
if 'watermelon' in favorite_fruits:
    print"You really like bananas!"
if 'banana' in favorite_fruits:
    print"You really like bananas!"

5-8 以特殊方式跟管理员打招呼

#5-8 以特殊方式跟管理员打招呼

lists = ['admin','Rihana','Rebecca','Jenny','Rose']
for name in lists:
    if name == 'admin':
        print"Hello admin, would you like to see a status report?"
    else:
        print("Hello " + name +", thank you for logging in again.")

5-9 处理没有用户的情形

#5-9 处理没有用户的情形

lists = ['admin','Rihana','Rebecca','Jenny','Rose']
for name in lists:
    if name == 'admin':
        print"Hello admin, would you like to see a status report?"
    else:
        print("Hello " + name +", thank you for logging in again.")
del lists[0]
del lists[0]
del lists[0]
del lists[0]
del lists[0]
if lists == []:
    print"We need to find some uses!"

5-10 检查用户名

#5-10 检查用户名

current_users = ['admin','rihana','rebecca','jenny','rose']
new_users = ['Rihana','Rebecca','Anna','Kristina','Gina']
for new_user in new_users:
    if new_user.lower() in current_users:
        print"需要输入别的用户名"
    else:
        print"这个用户名未被使用"

5-11 序数

#5-11 序数

lists = list(range(1, 10))
for i in lists:
    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')

你可能感兴趣的:(Python编程:从入门到实践)