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')