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

#5-1 条件测试
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
if car == 'subaru':
    print(car == 'subaru')
#5-2 更多的条件测试
#5-3 外星人颜色#1
alien_color = ['green']
if 'green' in alien_color:
    print('恭喜你获得了5个点')

print('版本2')
alien_color = ['red']
if 'green' in alien_color:
    print('恭喜你获得了5个点')
#5-4 外星人颜色#2
alien_color = ['green']
if 'green' in alien_color:
    print('恭喜你获得了5个点')
else:
    print('恭喜你获得10个点')

print('版本2')
alien_color = ['red']
if 'green' in alien_color:
    print('恭喜你获得了5个点')
else:
    print('恭喜你获得10个点')
#5-5 外星人颜色#3
alien_color = ['green']
if 'green' in alien_color:
    print('恭喜你获得了5个点')
elif 'yellow' in alien_color:
    print('恭喜你获得10个点')
else:
    print('恭喜你获得15个点')

print('版本2')
alien_color = ['red']
if 'green' in alien_color:
    print('恭喜你获得了5个点')
elif 'yellow' in alien_color:
    print('恭喜你获得10个点')
else:
    print('恭喜你获得15个点')

print('版本3')
alien_color = ['yellow']
if 'green' in alien_color:
    print('恭喜你获得了5个点')
elif 'yellow' in alien_color:
    print('恭喜你获得10个点')
else:
    print('恭喜你获得15个点')
#5-6 人生的不同阶段
age = 10
if age < 2:
    print("婴儿")
elif age < 4:
    print('蹒跚学步')
elif age < 13:
    print('儿童')
elif age < 20:
    print('青少年')
elif age < 65:
    print('成年人')
else:
    print('老年人')
#5-7 喜欢的水果
#5-8 以特殊方式跟管理员打招呼
names = ['ergou','tiezhu','shitou','admin','erya']
for name in names:
    if name == 'admin':
        print('Hello ' + name + ',would you like to see a status report?')
    else:
        print('Hello ' + name +',thank you for logging in again')
#5-9 处理没有用户的情形
names = []
if names:
    for name in names:
        if name == 'admin':
            print('Hello ' + name + ',would you like to see a status report?')
        else:
            print('Hello ' + name +',thank you for logging in again')
else:
    print('We need to find some users!')
#5-10 检查用户名
current_users = ['ergou','tiezhu','Shitou','admin','erya']
new_users = ['Ergou','xiaofeng','shitou','shuanzi','dongzi']
linshi = [] #临时储存全部小写的current_users里的元素
#小写的current_users里的元素
for name in current_users:    
    linshi.append(name.lower())

for name in new_users:
    if name.lower() in linshi:
        print(name + '已被使用,请输入别的用户名!')
    else:
        print('该用户名未被使用!')
#5-11 序数
numbers = list(range(1,10))
print (numbers)
for number in numbers:
    if number == 1:
        print(str(number)+'st')
    elif number == 2:
        print(str(number)+'nd')
    elif number == 3:
        print(str(number)+'rd')
    else:
        print(str(number)+'th')

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