第五章课后作业

Chapter Five

5-1 条件测试

course = "Operation System"
print("Is course == 'Operation System'? I predict True")
print(course == 'Operation System')

print("Is course == 'Computer Networking'? I predict False")
print(course == 'Computer Networking')

5-2 更多的条件测试

print("'Clear' == 'clear' ? : " + str("Clear" == "clear"))
print("'Clear' == 'Clear'.lower() ? : " + str("Clear" == "Clear".lower()))

print("2 == 3 ? :" + str(2 == 3))
print("2 != 3 ? :" + str(2 != 3))
print("2 > 3 ? : " + str(2 > 3))
print("2 < 3 ? : " + str(2 < 3))
print("2 >= 3 ? : " + str(2 >= 3))
print("2 <= 3 ? : " + str(2 <= 3))

print("2 != 3 and 2 > 3 ? : " + str(2 != 3 and 2 > 3))
print("2 != 3 or 2 > 3 ? : " + str(2 != 3 or 2 > 3))

nums = [1, 2, 3, 5, 6]
print("3 in list ? :" + str(3 in nums))
print("3 not in list ? : " + str(3 not in nums))

5-3 外星人颜色#1

# alien_color = 'yellow'
alien_color = 'green'
if alien_color == 'green':
    print("You shoot a green alien and got 5 points")

5-4 外星人颜色#2

# alien_color == "green"
alien_color = "yellow"
if alien_color == "green":
    print("You shoot a green alien and got 5 points")
else:
    print("Your shoot a strange alien and got 10 points")

5-5 外星人颜色#3

# alien_color = "green"
# alien_color = "red"
alien_color = "yellow"

if alien_color == "green":
    print("You shoot a green alien and got 5 points")
elif alien_color == "yellow":
    print("You shoot a yellow alien and got 10 points")
else:
    print("You shoot a red alien and got 15 points")

5-6 人生的不同阶段

age = 19
if age < 2:
    print("You are a baby")
elif age < 4:
    print("You are learning walking")
elif age < 13:
    print("You are a child")
elif age < 20:
    print("You are a teenager")
elif age < 65:
    print("You are an adult")
else:
    print("You are an elder")

5-7 喜欢的水果

favorite_fruits ={"apple", "banana", "pear"}
fruits = ["apple", "pear", "cherry", "banana", "pineapple"]
for fruit in fruits:
    if fruit in favorite_fruits:
        print("You really like " + fruit + "!")

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

users = ["Bob", "Alice", "Jack", "Eric", "admin"]

for user in users:
    if user == "admin":
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello " + user + ", thank you for logging in again")

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

users = ["Bob", "Alice", "Jack", "Eric", "admin"]

if users:
    for user in users:
        if user == "admin":
            print("Hello admin, would you like to see a status report?")
        else:
            print("Hello " + user + ", thank you for logging in again")
else:
    print("We need to find some users!")

while users:
    users.pop()

if users:
    for user in users:
        if user == "admin":
            print("Hello admin, would you like to see a status report?")
        else:
            print("Hello " + user + ", thank you for logging in again")
else:
    print("We need to find some users!")

5-10 检查用户名

current_users = ["yamato", "fubuki", "agaki", "shimakaze", "saratoga"]
new_users = ["Poi", "Ark Royal", "Warspite", "Fubuki", "Saratoga"]

for user in new_users:
    if user.lower() in current_users:
        print("User name '" + user + "'used, please try again!")
    else:
        print("User name '" + user + "'  available.")

5-11 序数

nums = list(range(1, 10))
for num in nums:
    if num == 1:
        print(str(num) + "st")
    elif num == 2:
        print(str(num) + "nd")
    elif num == 3:
        print(str(num) + "rd")
    else:
        print(str(num) + "th")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值