20211029
5-1
car = 'suzuki'
print('Is car == "suzuki"? I predict True')
print(car == 'suzuki')
car = 'Jaguar'
print('Is car == "Jaguar"? I predict False')
print(car == 'jaguar')
...
5-2
car = 'suzuki'
print('Is car == "suzuki"? I predict True')
print(car == 'suzuki')
car = 'Jaguar'
print('Is car == "Jaguar"? I predict False')
print(car == 'jaguar')
print('Is car == "Jaguar"? I predict True')
print(car.lower == 'jaguar')
x, y, z = 1, 2, 1
print(x ==z, x != y, y > z, x < y, y >= z,x <= y)
print((x ==z) and (x != y))
print((x != z) or (x != y))
food = ['duck','chicken']
print('duck' in food)
print('fish' not in food)
5-3
alien_color = 'green'
if alien_color =='green':
print('U got 5 points!')
alien_color = 'red'
if alien_color =='green':
print('U got 5 points!')
5-4
alien_color = 'green'
if alien_color =='green':
print('U got 5 points!')
else:
print('U got 10 points!')
alien_color = 'red'
if alien_color =='green':
print('U got 5 points!')
else:
print('U got 10 points!')
5-5
alien_color = 'green'
if alien_color =='green':
print('U got 5 points!')
elif alien_color =='yellow':
print('U got 10 points!')
else:
print('U got 15 points!')
alien_color = 'yellow'
if alien_color =='green':
print('U got 5 points!')
elif alien_color =='yellow':
print('U got 10 points!')
else:
print('U got 15 points!')
alien_color = 'green'
if alien_color =='red':
print('U got 5 points!')
elif alien_color =='yellow':
print('U got 10 points!')
else:
print('U got 15 points!')
5-6
age = 21
if age < 2:
print("he's a baby")
elif age < 4:
print("he's a toddler")
elif age < 13:
print("he's a kid")
elif age < 20:
print("he's a teenager")
elif age < 65:
print("he's a adult")
else:
print("he's a elder")
5-7
favorite_fruit = ['lychee', 'Mandrin', 'longan', 'plum']
if 'plum' in favorite_fruit:
print('U really like plums!')
if 'lychee' in favorite_fruit:
print('U really like litchis!')
if 'Mandrin' in favorite_fruit:
print('U really like Mandrin!')
if 'peach' in favorite_fruit:
print('U really like peaches!')
if 'apple' in favorite_fruit:
print('U really like apples!')
5-8
name = ['admin', 'I', 'L', 'W', 'X', 'Y']
for user in name:
if user == 'admin':
print('Hello admin, would U like to see a status report?')
else:
print('Hello ' + user + ', thank U for logging in again')
5-9
name = []
if name:
for user in name:
if user == 'admin':
print('Hello admin, would U like to see a status report?')
else:
print('Hello ' + user + ', thank U for logging in again')
else:
print('We need to finf some users!')
5-10/
current_users = ['S', 'I', 'L', 'W', 'X', 'Y']
new_users = ['s', 'Z', 'x', 'G', 'B']
current_users_lower = [cur_user.lower() for cur_user in current_users]
for user in new_users:
if user.lower() in current_users_lower:
print('name used, plz use another')
else:
print('name unused')
5-11
nums = list(range(1,10))
for num in nums:
if num == 1:
print('1st')
elif num == 2:
print('2nd')
elif num == 3:
print('3rd')
else:
print(str(num) + 'th')
5-12
...
5-13
...
Python编程:从入门到实践 练习答案 Chapter05
最新推荐文章于 2024-03-08 20:31:31 发布