高级编程技术 - 第五章练习

5-3 外星人颜色1

# alien_color_1_1.py
alien_color = 'green'
if alien_color == 'green' :
    print("You've got five points.")

Output
You’ve got five points.

# alien_color_1_2.py
alien_color = 'yellow'
if alien_color == 'green' :
    print("You've got five points.")

Output
None

5-4 外星人颜色2

# alien_color_2_1.py
alien_color = 'green'
if alien_color == 'green':
    print("You've got five points.")
else:
    print("You've got ten points.")

Output
You’ve got five points.

# alien_color_2_2.py
alien_color = 'yellow'
if alien_color == 'green':
    print("You've got five points.")
else:
    print("You've got ten points.")

Output
You’ve got ten points.

5-5 外星人颜色3

# alien_color_2_1.py
alien_color = 'red'
if alien_color == 'green':
    print("You've got five points.")
elif alien_color == 'yellow':
    print("You've got ten points.")
else:
    print("You've got fifteen points.")

Output
None

5-6 人生的不同阶段

# ages.py
age = 20
if age < 2 :
    print("He's a baby.")
elif age < 4 :
    print("He's learning to walk.")
elif age < 13 :
    print("He's a child.")
elif age < 20 :
    print("He's a teenager.")
elif age < 65 :
    print("He's a adult.")
else:
    print("He's a old man.")

Output
None

5-7 喜欢的水果

# favorite_fruits.py
favorite_fruits = ['apple','banana','peach','strawberry','pear']
if 'apple' in favorite_fruits:
    print("You really like apple!")
if 'lemon' in favorite_fruits:
    print("You really like lemon!")
if 'strawberry' in favorite_fruits:
    print("You really like strawberry!")
if 'plum' in favorite_fruits:
    print("You really like plum!")
if 'pear' in favorite_fruits:
    print("You really like pear!")

Output
You really like apple!
You really like strawberry!
You really like pear!

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

# special_hello.py
administrators = ['admin','user1','user2','user3','user4']
for admin in administrators:
    if admin == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello "+ admin + ", thank you for logging in again.")

Output
Hello admin, would you like to see a status report?
Hello user1, thank you for logging in again.
Hello user2, thank you for logging in again.
Hello user3, thank you for logging in again.
Hello user4, thank you for logging in again.

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

# no_user.py
administrators = []
if not administrators:
    print("We need to find some users!")

Output
We need to find some users!

5-10 检查用户名

# check_user.py
current_users = ['admin','user1','user2','user3','user4']
new_users = ['admin','user5','user6','user2','user7']
for new_user in new_users:
    if new_user.lower() in current_users or new_user.upper() in current_users:
        print(new_user+" is used, choose another name.")
    else:
        print(new_user + " is available.")

Output
admin is used, choose another name.
user5 is available.
user6 is available.
user2 is used, choose another name.
user7 is available.

5-11 序数

# list.py
lists = list(range(1,10))
for i in lists:
    if i == 1:
        print("1st")
    elif i == 2:
        print("2nd")
    elif i == 3:
        print("3rd")
    else:
        print(str(i)+"th")

Output
1st
2nd
3rd
4th
5th
6th
7th
8th
9th

你可能感兴趣的:(高级编程技术)