5-3 外星人颜色#1
alien_color = 'green'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
alien_color = 'red'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
输出:
You have killed green alien and got 5 points!
5-4 外星人颜色#2
alien_color = 'green'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 10 points!")
alien_color = 'red'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 10 points!")
输出:
You have killed green alien and got 5 points!
You have killed red alien and got 10 points!
5-5 外星人颜色#3
alien_color = 'green'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
elif alien_color == 'yellow':
print("You have killed " + alien_color + " alien" + " and got 10 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 15 points!")
alien_color = 'yellow'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
elif alien_color == 'yellow':
print("You have killed " + alien_color + " alien" + " and got 10 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 15 points!")
alien_color = 'red'
if alien_color == 'green':
print("You have killed " + alien_color + " alien" + " and got 5 points!")
elif alien_color == 'yellow':
print("You have killed " + alien_color + " alien" + " and got 10 points!")
else:
print("You have killed " + alien_color + " alien" + " and got 15 points!")
输出:
You have killed green alien and got 5 points!
You have killed yellow alien and got 10 points!
You have killed red alien and got 15 points!
5-6 人生的不同阶段
age = 34
if age < 2:
print("You are a baby.")
elif age >= 2 and age < 4:
print("You are toddling.")
elif age >= 4 and age < 13:
print("You are a child.")
elif age >= 13 and age < 20:
print("You are a teenager.")
elif age >= 20 and age < 65:
print("You are an adult.")
else:
print("You are an old man.")
输出:
You are an adult.
5-8 以特殊方式跟管理员打招呼
users = ['Mike', 'John', 'admin', 'Amy', 'Ten']
for user in users:
if user == 'admin':
print("Hello " + user + ", would you like to see a status report?")
else:
print("Hello " + user + ", thank you for logging in again.")
输出:
Hello Mike, thank you for logging in again.
Hello John, thank you for logging in again.
Hello admin, would you like to see a status report?
Hello Amy, thank you for logging in again.
Hello Ten, thank you for logging in again.
5-10 检查用户名
current_users = ['Mike', 'John', 'admin', 'Amy', 'Ten']
new_users = ['Sarah', 'Newton', 'John', 'Ten', 'Hua']
lower_users = []
for user in current_users:
lower_users.append(user.lower())
for user in new_users:
if user.lower() in lower_users:
print("This name has been used, please enter another username.")
else:
print("The name has not been used yet.")
输出:
The name has not been used yet.
The name has not been used yet.
This name has been used, please enter another username.
This name has been used, please enter another username.
The name has not been used yet.
5-11 序数
nums = list(range(1, 10))
for num in nums:
if num == 1:
print(str(num) + "st")
elif num == 2:
print(str(num) + "nd")
elif num == 3:
print(str(num) + "rd")
else:
print(str(num) + "th")
输出:
1st
2nd
3rd
4th
5th
6th
7th
8th
9th