《Python编程从入门到实践》第二版?
练习题代码实现 第五章
#P69
#5-1条件测试
car='subaru'
print("Is car =='subaru'? I predict ture.")
print(car=='subaru')
print("\nIs car =='audi'? I predict false.")
print(car=='audi')
#5-2更多条件测试
a='abc'
b='abce'
c='Abc'
print("a==b? i predict false" )
print(a==b)
print("a==c? i predict false")
print(a==c)
print(a.lower()==c.lower())
#5-3外星人颜色
alien_color=['green','yellow','red']
if 'green' in alien_color:
print("you got 5")
#5-4外星人颜色2
alien_color=['green','yellow','red']
if'green'in alien_color:
print("u got 5")
else:
print("u got 10")
#5-5外星人颜色3
alien_color=['green','yellow','red']
if'green'in alien_color:
print("u got 5")
elif'yellow'in alien_color:
print("u got 10")
else:
print("u got 15")
#5-6人生的不同阶段
age=24
if age<2:
print("she is a baby")
elif age<4:
print("she is a child")
elif age<13:
print("she is a children")
elif age<20:
print("she is a teenger")
elif age<65:
print("she is a audlt")
else:
print("she is a elder")
#5-7喜欢的水果
favorite_fruits=['banana','kiwifruit','watermelon']
if 'banana' in favorite_fruits:
print("you really like bananas!")
if 'kiwifruit' in favorite_fruits:
print("you really like kiwifruit!")
if 'watermelon' in favorite_fruits:
print("you really like watermelon!")
#5-8以特殊方式跟管理员打招呼
user_names=['a','b','c','d','admin']
print(user_names)
print("欢迎登陆!")
for user_name in user_names:
if user_name=='admin' :#'admin' in user_names:
print("Hello admin,would you like to see a status report?")
else:
print("hello" ,user_name, "thank you for logging in again.")
#5-9处理没有用户的情形
user=[]
if user:
print("welcome!")
else:
print("we need to find some users!")
#5-10检查用户名
current_users = ['锦觅','旭凤','狐狸仙','润玉','admin']
new_users = ['旭凤','锦觅','邓论','杨紫','wo']
for value in new_users:
value = value.lower()
if value in current_users:
print(value,'已被使用!请输入别的用户名:')
else:
print(value,'此用户名可以使用!')
#5-11序数
numbers = list(range(1,11))
print(numbers)
for value in numbers:
value = str(value)
if value == '1':
print(value + 'st')
elif value == '2':
print(value + 'nd')
elif value == '3':
print(value + 'rd')
else:
print(value + 'th')
|