5-3 外星人颜色#1
alien_color = 'green'
if(alien_color == 'green'):
print("You get 5 points!")
alien_color = 'yellow'
if(alien_color == 'green'):
print("You get 5 points!")
输出
You get 5 points!
5-4 外星人颜色#2
alien_color = 'green'
if(alien_color == 'green'):
print("You get 5 points!")
else:
print("You get 10 points!")
alien_color = 'yellow'
if(alien_color == 'green'):
print("You get 5 points!")
else:
print("You get 10 points!")
输出
You get 5 points!
You get 10 points!
5-5 外星人颜色#3
alien_color = 'green'
if(alien_color == 'green'):
print("You get 5 points!")
elif(alien_color == 'yellow'):
print("You get 10 points!")
elif(alien_color == 'red'):
print("You get 15 points!")
alien_color = 'yellow'
if(alien_color == 'green'):
print("You get 5 points!")
elif(alien_color == 'yellow'):
print("You get 10 points!")
elif(alien_color == 'red'):
print("You get 15 points!")
alien_color = 'red'
if(alien_color == 'green'):
print("You get 5 points!")
elif(alien_color == 'yellow'):
print("You get 10 points!")
elif(alien_color == 'red'):
print("You get 15 points!")
输出
You get 5 points!
You get 10 points!
You get 15 points!
5-6 人生的不同阶段
age = 17
if(age < 2):
print("This person is a baby")
elif(age < 4):
print("This person is learning walking")
elif(age < 13):
print("This person is a child")
elif(age < 20):
print("This person is a teenage")
elif(age < 65):
print("This person is an adult")
else:
print("This person is an elder")
输出
This person is a teenage
5-7 喜欢的水果
favorite_fruits = ['apple','watermelon','banana']
if('apple' in favorite_fruits):
print("You really like apples!")
if('banana' in favorite_fruits):
print("You really like bananas!")
if('watermelon' in favorite_fruits):
print("You really like watermelons!")
if('peer' in favorite_fruits):
print("You really like peers!")
if('orange' in favorite_fruits):
print("You really like oranges!")
输出You really like apples!
You really like bananas!
You really like watermelons!
5-8 以特殊方式跟管理员打招呼
users = ['admin','user1','user2','user3','Eric']
for user in users:
if(user == 'admin'):
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user + ", thank you for logging in again")
输出
Hello admin, would you like to see a status report?
Hello user1, thank you for logging in again
Hello user2, thank you for logging in again
Hello user3, thank you for logging in again
Hello Eric, thank you for logging in again
5-9 处理没有用户的情形
users = []
if(users):
for user in users:
if(user == 'admin'):
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user + ", thank you for logging in again")
else:
print("We need to find some users!")
输出
We need to find some users!
5-10 检查用户名
current_users = ['user1','user2','user3','user4','Eric']
new_users = ['user5','user6','user7','User1','eRic']
for new_user in new_users:
for current_user in current_users:
if(new_user.upper() == current_user.upper()):
print("Refuse user name " + new_user)
输出
Refuse user name User1
Refuse user name eRic
5-11 序数
numbers = [1,2,3,4,5,6,7,8,9]
for number in numbers:
if(number == 1):
print("1st")
elif(number == 2):
print("2nd")
elif(number == 3):
print("3rd")
else:
print(str(number)+"th")
输出
1st
2nd
3rd
4th
5th
6th
7th
8th
9th