5-1
#coding:gbk
#5-1条件测试
a = 1
print('Is a == 2? I predict False')
print(a == 2)
print('Is a == 1? I predict True')
print(a == 1)
5-2
#coding:gbk
#5-2更多的条件测试
a = 'Johnny'
b = 'Timmy'
print('If a == b? I predict False.')
print(a == b)
print('\nIf a.low() == "johnny"? I predict True.')
print(a.lower() == 'johnny')
print(a)
x = 310
y = 417
print('\nIf x == y? I predict False.')
print(x == y)
print('\nIf x != y? I predict True.')
print(x != y)
print('\nIf x > y? I predict False.')
print(x > y)
print('\nIf x < y? I predict True.')
print(x < y)
print('\nIf x >= y? I predict False.')
print(x >= y)
print('\nIf x <= y? I predict True.')
print(x <= y)
print('\nif x > 0 and x < 100? I predict False.')
print(x > 0 and x < 100)
print('\nif x > 0 or x < 100? I predict True.')
print(x > 0 or x < 100)
print('\n')
list = [1, 2, 4, 5]
print(3 in list)
print(3 not in list)
5-3
#coding:gbk
#5-3外星人颜色
alien_color = 'green'
if alien_color == 'green':
print('You get 5 points.')
if alien_color == 'yellow':
print('You get 5 points.')
5-6
#coding:gbk
#5-6人生的不同阶段
age = input('输入你的年龄:')
age = int(age)
if age < 2:
print('你是婴儿.')
elif age < 4:
print('你在蹒跚学步.')
elif age < 13:
print('你是儿童.')
elif age < 20:
print('你是青少年.')
elif age < 65:
print('你是成年人.')
else:
print('你是老年人.')
5-7
#coding:gbk
#5-7喜欢的水果
favorite_fruits = ['橘子', '芒果', '荔枝']
if '香蕉' in favorite_fruits:
print('你喜欢吃香蕉')
else:
print('你不喜欢吃香蕉')
if '苹果' in favorite_fruits:
print('你喜欢吃苹果')
else:
print('你不喜欢吃苹果')
if '雪梨' in favorite_fruits:
print('你喜欢吃雪梨')
else:
print('你不喜欢吃雪梨')
if '荔枝' in favorite_fruits:
print('你喜欢吃荔枝')
else:
print('你不喜欢吃荔枝')
if '橘子' in favorite_fruits:
print('你喜欢吃橘子')
else:
print('你不喜欢吃橘子')
5-8
#coding:gbk
#5-8以特殊方式跟管理员打招呼
client_list = ['Johnny', 'Timmy', 'Tom', 'Jerry']
#while client_list:
# client_list.pop()
if client_list:
for client in client_list:
if client == 'Johnny':
print('Hello Johnny, would you like to see a status report?')
else:
print('Hello ' + client + ', thank you for logging in again')
else:
print("We need to find some users!")
5-10
#coding:gbk
#5-10检查用户名
current_users = ['Johnny', 'Timmy', 'Tom', 'Jerry', 'Lily']
new_users = ['JOHNNY', 'Black', 'Green', 'Jerry', 'White']
current_users_lower = [user.lower() for user in current_users]
for user in new_users:
if user.lower() in current_users_lower:
print('用户名已被占用')
else:
print('用户名合法')
5-11
#coding:gbk
#5-11序数
nums = range(1, 10)
for n in nums:
if n == 1:
print('1st')
elif n == 2:
print('2nd')
elif n == 3:
print('3rd')
else:
print(str(n) + 'th')
6-1 & 6-7
#coding:gbk
#6-1人
people1 = {
'friend_name' : 'Johnny',
'last_name' : 'Huang',
'age' : 26,
'city' : 'DDG',
}
for key, value in people1.items():
print(key + ':' + str(value))
#6-7 列表嵌套字典
people2 = {
'friend_name' : 'Timmy',
'last_name' : 'Xu',
'age' : 24,
'city' : 'SHA',
}
people3 = {
'friend_name' : 'Lily',
'last_name' : 'Blue',
'age' : 18,
'city' : 'Hang',
}
peoples = [people1, people2, people3]
for people in peoples:
print(people)
6-2
#coding:gbk
#6-2喜欢的数字
favorite_num = {
'Johnny' : 6,
'Timmy' : 2,
'Tom' : 529,
'Jerry' : 417,
'Lily' : 310,
}
for name, num in favorite_num.items():
print(name + "'s favorite num is " + str(num))
6-5
#coding:gbk
#6-5河流
rivers = {
'尼罗河' : '埃及、苏丹、埃塞俄比亚',
'亚马逊' : '秘鲁、厄瓜多尔、哥伦比亚',
'长江' : '中国',
}
for river, country in rivers.items():
print(river + '流经' + country + '。')
6-9
#coding:gbk
#6-9喜欢的地方(字典嵌套列表)
favorite_places = {
'Johnny' : ['a' , 'b', 'c'],
'Timmy' : ['b', 'c', 'd'],
'Tom' : ['a' , 'b', 'd'],
}
for name, cities in favorite_places.items():
print(name + "'s favorite cities are: ", end = '')
print(cities)
6-11
#coding:gbk
#6-11城市(字典嵌套字典)
cities = {
'北京': {
'国家' : '中国',
'人口' : '很多很多',
'备注' : '政治文化经济中心',
},
'巴黎': {
'国家' : '法国',
'人口' : '不知道',
'备注' : '浪漫之都',
},
'东京': {
'国家' : '日本',
'人口' : '不清楚',
'备注' : '日本首都',
},
}
for city, message in cities.items():
print(city + '所属国家是' + message['国家'] + ',')
print('人口总量' + message['人口'] + ',')
print('是' + message['备注'] + '。')
print('\n')