Python编程 从入门到实践 第五章习题

5-4 外星人的颜色#2:

alien_color = 'green'    #version 1, 执行if语句
#alien_color = 'yellow'   #version 2, 执行else语句
if alien_color == 'green':
    print("You have win 5 scores!")
else:
    print("You have win 10 scores!")

输出:
You have win 5 scores!            #version 1   
You have win 10 scores!           #version 2

5-5 外星人的颜色#3:

alien_color = 'green'              #version1
#alien_color = 'yellow'            #version2
#alien_color = 'red'               #version3
if alien_color == 'green':
    print("You have win 5 scores!")
elif alien_color == 'yellow':
    print("You have win 10 scores!")
elif alien_color == 'red':
    print("You have win 15 scores!")
输出:
You have win 5 scores!             #version1
You have win 10 scores!            #version2
You have win 15 scores!            #version3

5-7 喜欢的水果:

favorite_fruits = ['apple', 'orange', 'melon']
if 'apple' in favorite_fruits:
    print('You really like apple!')
if 'peer' in favorite_fruits:
    print("You really like peer!")
if 'orange' in favorite_fruits:
    print("You really like orange!")
if 'banana' in favorite_fruits:
    print("You really like banana!")
if 'melon' in favorite_fruits:
    print("You really like melon!")
输出:
You really like apple!
You really like orange!
You really like melon!
5-8 以特殊方式和管理员打招呼
names = ['admin','Eric','Alex','Leo', 'Mike']
for name in names:
    if name == 'admin':
        print("Hello admin, would you like to see a status report ?")
    else:
        print("Hello " + name + ", thank you for logging in again!")
输出:
Hello admin, would you like to see a status report ?
Hello Eric, thank you for logging in again!
Hello Alex, thank you for logging in again!
Hello Leo, thank you for logging in again!
Hello Mike, thank you for logging in again!
5-9 处理没有客户的情形
names = ['admin','Eric','Alex','Leo', 'Mike']
for name in names:
    if name == 'admin':
        print("Hello admin, would you like to see a status report ?")
    else:
        print("Hello " + name + ", thank you for logging in again!")
names = []
if names:
    for name in names:
        print("Hello!" + name)
else:
    print("\nWe need some users!")
输出
Hello admin, would you like to see a status report ?
Hello Eric, thank you for logging in again!
Hello Alex, thank you for logging in again!
Hello Leo, thank you for logging in again!
Hello Mike, thank you for logging in again!

We need some users!

5-10 检查用户名

current_name = ['Alex', 'Mike', 'John', 'Eric', 'Leo']
new_users = ['alex',  'Simth','JOHN', 'Alice', 'Lau']

for name in new_users:
    for names in current_name:
        if name.lower() == names.lower():
            print("The name has been registered,you should try again.")
            break
    else:                                   ############# 按理解,应该会报错!!!  可是没有??
        print("The name is not used!")

输出:

The name has been registered,you should try again.
The name is not used!
The name has been registered,you should try again.
The name is not used!
The name is not used!

存疑!!

else语句和第二个for 语句并列,并不是和if并列,但输出结果正常且符合要求,语法疑问?

5-11 序数

numbers = list(range(1,10))

for number in numbers:
    if number == 1:
        print('1st')
    elif number == 2:
        print('2nd')
    elif number == 3:
        print('3rd')
    else:
        print(str(number) + 'th')

输出:

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










猜你喜欢

转载自blog.csdn.net/sysu_alex/article/details/79609617