高级编程技术第五次作业

时间:2021-11-21 21:58:08

第五章 if语句

5-1 条件测试

name = "Peter"
print ("Is name == Peter? ") print (name == "Peter")

5-2 更多的测试条件

name1 = "Peter"
name2 = "Bob"
print ("Is name1 == Peter and name2 == Bob? ")
print (name1 == "Peter" and name2 == "Bob")
print ("Is 2 bigger than 0? ")
print (2 > 0)

5-3 外星人颜色#1

alien_color = "green"
if(alien_color == "green"):
    print ("You get 5 points")
alien_color = "yellow"
if(alien_color == "green"):
    print ("You get 5 points")
else:
    print ("You get 0 points")

5-4 外星人颜色#2

alien_color = "red"
if alien_color == "green":
    print ("You get 5 points")
else:
    print ("You get 10 points")

5-5 外星人颜色#3

alien_color = "red"
if alien_color == "green":
    print ("You get 5 points")
elif alien_color == "yellow":
    print ("You get 10 points")
elif alien_color == "red":
    print ("You get 15 points")

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

names = ["admin", "Bob", "Cindy", "David", "Eric"]
for name in names:
    if name == "admin":
        print ("Hello admin, would you like to see a status report?")
    else:
        print ("Hello Eric, thank you for logging in again")

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

names = ["admin", "Bob", "Cindy", "David", "Eric"]
for name in names:
    if name == "admin":
        print ("Hello admin, would you like to see a status report?")
    else:
        print ("Hello Eric, thank you for logging in again")

if names:
    print ("There are users in names")
    del names
    print ("We need to find some users!")
else:
    print ("We need to find some users!")

5-11 序数

numbers = list(range(1, 10))
for number in numbers:
    print (str(number), end = "")
    if number == 1:
        print ("st")
    elif number == 2:
        print ("nd")
    elif number ==3:
        print ("rd")
    else:
        print ("th")