5-1
car = 'subaru'
print('Is car == "subaru"? I predict True') #个人习惯优先使用单引号
print(car == 'subaru')
print('Is car == "bmw"? I predict True')
print(car == 'bmw')
print('Oops,I lose')
5-2
stra = '今天天气真好'
strb = '今天天气不好'
print(stra == strb)
stra = 'Today,I Should'
strb = 'tomrrorw,i will'
if stra != strb:
print(stra.lower())
5-3
#aline_color = 'Red'
aline_color = 'Green'
if aline_color == 'Green':
print('恭喜!,你杀了一个绿色的,+5分!')
5-4
#alien_color = 'Blue'
alien_color = 'Red'
if alien_color == 'Blue':
print('恭喜!,你杀了一个蓝色的,+5分!')
else:
print('任务失败!')
5-5
#alien_color = 'Blue'
#alien_color = 'Red'
alien_color = 'Green'
if alien_color == 'Blue':
print('恭喜!,你杀了一个蓝色的,+5分!')
elif alien_color == 'Red':
print('恭喜!,你杀了一个红色的,+5分!')
else:
print('恭喜!,你杀了一个绿色的,+15分!')
5-6
age = 48
if age < 2:
print('婴儿')
elif age < 4:
print('幼儿')
elif age <13:
print('儿童')
elif age < 20:
print('青少年')
elif age < 65:
print('成年人')
elif age > 65:
print('老年人')
5-7
fruits = ['苹果','梨','香蕉']
str1 = '苹果'
if str1 in fruits: #元素可以直接当条件判断
print(f"you really like {str1}")
5-8
users = ['001','002','admin','003','004']
for user in users:
if user == 'admin':
print('Hello,admin!would u like to see a status report?')
else:
print('hello' + user + 'thank you')
5-9
#users = ['001','002','admin','003','004']
users = []
if users: #先使用if判断列表内容是否为空,有内容为True,空的则为False
for user in users:
if user == 'admin':
print('Hello,admin!would u like to see a status report?')
else:
print('hello' + user + 'thank you')
else:
print('we need to find some users')
5-10-1
current_users = ['001','002','admin','003','004']
new_users = ['aaa','bbb','ccc','admin','004']
for new_user in new_users:
if new_user in current_users:
print(new_user + '该用户名已被使用!')
else:
print(new_user + '请继续,此用户名并未被使用')
5-10-2
current_users = ['aaa','BBb','ccC','admin','004']
current_users_lower = ['aaa','bbb','ccc','admin','004']
new_users = ['001','CCC','adMin','003','004']
for new_user in new_users:
if new_user.lower() in current_users_lower:
print(new_user + '用户名已存在!')
else:
print(new_user + '请继续,此用户名未被使用')