Python编程:从入门到实践的动手试一试练习答案(第五章)

以下内容非标准答案,是我个人练习内容,仅供参考:

如有不准确,希望指出

# ~ #5-1
foods = ['苹果','梨','橘子']
for food in foods:
    if food == '橘子':
        print(food + '橘子需要剥皮才能吃!')
    else:
        print(food + '洗干净外皮就可以吃!')
# ~ #5-2-1
name_1 = 'Peter'
name_2 = 'peter'

if name_1 == name_2:
    print('他们的名字一样')
else:
    print('他们的名字不一样')
# ~ #5-2-2
name_1 = 'Peter'
name_2 = 'peter'

if name_1.lower() == name_2:
    print('他们的名字一样')
else:
    print('他们的名字不一样')
# ~ #5-2-3    
age_1 = 28
age_2 = 18

print(age_1 == age_2)
print(age_1 != age_2)
print(age_1 > age_2)
print(age_1 < age_2)
print(age_1 >= age_2)
print(age_1 <= age_2)
# ~ #5-2-4
age_1 = 28
age_2 = 18

print(age_1 < 30 and age_2 < 20)
print(age_1 > 30 or age_2 < 20)
# ~ #5-2-5
names = ['Peter','charles','王果','甜心520']
print('Peter' in names)
# ~ #5-2-6
names = ['Peter','charles','王果','甜心520']
print('peter' in names)
# ~ #5-3-1
alien_color = ['green','yellow','red']
if 'green' in alien_color:
    print('恭喜你' + '攻击green' + '获得5点')
# ~ #5-3-2  (if内容不在变量中,所以没有输出)
alien_color = ['green','yellow','red']
if 'blue' in alien_color:
    print('抱歉,你未获得积点')
# ~ #5-4-1
alien_color = ['green','yellow','red']
if 'green' in alien_color:
    print('恭喜你获得5点')
else:
    print('恭喜你获得10点')
# ~ #5-4-2
alien_color = ['green','yellow','red']
if 'blue' in alien_color:
    print('恭喜你获得5点')
else:
    print('恭喜你获得10点')
# ~ #5-5-1
alien_color = ['green','yellow','red']
if 'green' in alien_color:
    print('恭喜你获得5点')
elif 'yellow' in alien_color:
    print('恭喜你获得10点')
else:
    print('恭喜你获得15点')
# ~ #5-5-2
alien_color = ['green','yellow','red']
if 'blue' in alien_color:
    print('恭喜你获得5点')
elif 'yellow' in alien_color:
    print('恭喜你获得10点')
else:
    print('恭喜你获得15点')
# ~ #5-5-3
alien_color = ['green','yellow','red']
if 'blue' in alien_color:
    print('恭喜你获得5点')
elif 'black' in alien_color:
    print('恭喜你获得10点')
else:
    print('恭喜你获得15点')
# ~ #5-6
age = 35
if age < 2:
    print('婴儿')
elif 2 <= age < 4:
    print('蹒跚学步')
elif 4 <= age < 13:
    print('儿童')
elif 13 <= age < 20:
    print('青少年')
elif 20 <= age < 65:
    print('成年人')
else:
    print('老年人')
# ~ #5-7
favorite_fruits = ['apple','banana','grape','pomelo','pear']
if 'apple' in favorite_fruits:
    print('You really like apple')
if 'banana' in favorite_fruits:
    print('You really like banana')
if 'grape' in favorite_fruits:
    print('You really like grape')
if 'pomelo' in favorite_fruits:
    print('You really like pomelo')
if 'pear' in favorite_fruits:
    print('You really like pear')
# ~ #5-8
users = ['admin','charles','lisa','alian','agnes']

for user in users:
    if user == 'admin':
        print(
        'Hello ' + user + ',would you like to see a status report?'
        )
    else:
        print(
        'Hello ' + user +',thank you for logging in again'
        )
# ~ #5-9
users = []
if users:
    for user in users:
        if user == 'admin':
            print(
            'Hello ' + user + ',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 = ['admin','charles','lisa','alian','agnes']
new_users = ['admin','charles','emma','julien','agnes']
for new_user in new_users:
    if new_user in current_users:
        print(
        '用户名: ' + new_user + ' 已经被使用,请更换'
        )
    else:
        print(
        '用户名: ' + new_user + ' 可以被使用'
        )
# ~ #5-11
nums = [1,2,3,4,5,6,7,8,9]
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') 

你可能感兴趣的:(Python编程:从入门到实际,动手试一试,答案)