"""5.1 条件测试"""
fruit = 'apple'
print("Is fruit == 'apple'? I predict True.")
print(fruit == 'apple')
print("Is fruit == 'pear'? I predict Fsldr.")
print(fruit == 'pear')
fruit = 'banana'
print("\nIs fruit == 'banana'? I predict True.")
print(fruit == 'banana')
print("Is fruit == 'watermelon'? I predict Fsldr.")
print(fruit == 'watermelon')
animal = 'dog'
print("\nIs animal == 'dog'? I predict True.")
print(animal == 'dog')
print("Is annimal == 'duck'? I predict Fsldr.")
print(animal == 'duck')
animal = 'monkey'
print("\nIs animal == 'monkey'? I predict True.")
print(animal == 'monkey')
print("Is annimal == 'panda'? I predict Fsldr.")
print(animal == 'panda')
animal = 'ant'
print("\nIs animal == 'ant'? I predict True.")
print(animal == 'ant')
print("Is annimal == 'elephant'? I predict Fsldr.")
print(animal == 'elephant')
"""5.2 更多的条件测试"""
str1 = 'hello'
str2 = 'hi'
print("Is str1 == str2?")
print(str1 == str2)
name = 'Andy'
another_name = 'andy'
print("name == another_name?")
print(name.lower() == another_name)
print("\n2 == 2?")
print(2 == 2)
print("2 != 2?")
print(2 != 2)
print("3 > 2?")
print(3 > 2)
print("3 < 2?")
print(3 < 2)
print("3 >= 2?")
print(3 >= 2)
print("3 <= 2?")
print(3 <= 2)
number_1 = 99
number_2 = 199
if(number_1 > 50 and number_2 > 50):
print("\nTwo numbers are bigger than 50")
else:
print("\nNot all of these two numbers are bigger than 50")
if(number_1 > 100 or number_2 < 200):
print("One of numbers fix the condition.")
else:
print("Both do not fix the condition.")
animal = ['dog','duck','goose','ant']
print("\nIs ant in the list?")
print('ant' in animal)
print("Is monkey in the list?")
print('monkey' in animal)
"""5.3 外星人颜色#1"""
alien_color = 'green'
if alien_color == 'green':
print("You get 5 points.")
alien_color = 'red'
if alien_color == 'green':
print("You get 5 points.")
"""5.4 外星人颜色#2"""
alien_color = 'green'
if alien_color == 'green':
print("You get 5 points because of killing the alien.")
else:
print("You get 10 points.")
alien_color = 'red'
if alien_color == 'green':
print("You get 5 points.")
else:
print("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.")
else:
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.")
else:
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.")
else:
print("You get 15 points.")
"""5.6 人生的不同阶段"""
age = 18
if age < 2:
print("He is a baby.")
elif age >= 2 and age < 4:
print("He is learning to walk.")
elif age >= 4 and age <13:
print("He is a child")
elif age >= 13 and age < 20:
print("He is a teenager.")
elif age >= 20 and age < 65:
print("He is an adult.")
else:
print("He is an elder.")
"""5.7 喜欢的水果"""
favorite_fruit = ['watermelon','grapes','banana','cherry']
if 'watermelon' in favorite_fruit:
print("You really like watermelon.")
if 'apple' in favorite_fruit:
print("You really like apple.")
if 'grapes' in favorite_fruit:
print("You really like grapes.")
if 'pear' in favorite_fruit:
print("You really like pear.")
if 'cherry' in favorite_fruit:
print("You really like cherry.")
"""5.8 以特殊方式跟管理员打招呼"""
user_names = ['admin','Bob','Andy','Amy','Mike']
for user in user_names:
if user == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user +", thank you for logging in again")
"""5.9 处理没有用户的情形"""
user_names = ['admin','Bob','Andy','Amy','Mike']
for user in user_names:
if user == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user +", thank you for logging in again")
user_names.clear()
if len(user_names) == 0:
print("We need to find some users!")
"""5.10 检查用户名"""
current_users = ['Jackson','Bob','Andy','Amy','Mike']
new_users = ['Bob','Lily','Susan','Andy','David']
for i in range(0,5):
current_users[i] = current_users[i].lower()
for new_user in new_users:
if new_user.lower() in current_users:
print("The name has been used. Please input another name.")
else:
print("The name has not been used.")
"""5.11 序数"""
numbers = ['1','2','3','4','5','6','7','8','9']
for number in numbers:
if number == '1':
print(number + "st")
elif number == '2':
print(number + "nd")
elif number == '3':
print(number + "rd")
else:
print(number + "th")
fruit = 'apple'
print("Is fruit == 'apple'? I predict True.")
print(fruit == 'apple')
print("Is fruit == 'pear'? I predict Fsldr.")
print(fruit == 'pear')
fruit = 'banana'
print("\nIs fruit == 'banana'? I predict True.")
print(fruit == 'banana')
print("Is fruit == 'watermelon'? I predict Fsldr.")
print(fruit == 'watermelon')
animal = 'dog'
print("\nIs animal == 'dog'? I predict True.")
print(animal == 'dog')
print("Is annimal == 'duck'? I predict Fsldr.")
print(animal == 'duck')
animal = 'monkey'
print("\nIs animal == 'monkey'? I predict True.")
print(animal == 'monkey')
print("Is annimal == 'panda'? I predict Fsldr.")
print(animal == 'panda')
animal = 'ant'
print("\nIs animal == 'ant'? I predict True.")
print(animal == 'ant')
print("Is annimal == 'elephant'? I predict Fsldr.")
print(animal == 'elephant')
"""5.2 更多的条件测试"""
str1 = 'hello'
str2 = 'hi'
print("Is str1 == str2?")
print(str1 == str2)
name = 'Andy'
another_name = 'andy'
print("name == another_name?")
print(name.lower() == another_name)
print("\n2 == 2?")
print(2 == 2)
print("2 != 2?")
print(2 != 2)
print("3 > 2?")
print(3 > 2)
print("3 < 2?")
print(3 < 2)
print("3 >= 2?")
print(3 >= 2)
print("3 <= 2?")
print(3 <= 2)
number_1 = 99
number_2 = 199
if(number_1 > 50 and number_2 > 50):
print("\nTwo numbers are bigger than 50")
else:
print("\nNot all of these two numbers are bigger than 50")
if(number_1 > 100 or number_2 < 200):
print("One of numbers fix the condition.")
else:
print("Both do not fix the condition.")
animal = ['dog','duck','goose','ant']
print("\nIs ant in the list?")
print('ant' in animal)
print("Is monkey in the list?")
print('monkey' in animal)
"""5.3 外星人颜色#1"""
alien_color = 'green'
if alien_color == 'green':
print("You get 5 points.")
alien_color = 'red'
if alien_color == 'green':
print("You get 5 points.")
"""5.4 外星人颜色#2"""
alien_color = 'green'
if alien_color == 'green':
print("You get 5 points because of killing the alien.")
else:
print("You get 10 points.")
alien_color = 'red'
if alien_color == 'green':
print("You get 5 points.")
else:
print("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.")
else:
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.")
else:
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.")
else:
print("You get 15 points.")
"""5.6 人生的不同阶段"""
age = 18
if age < 2:
print("He is a baby.")
elif age >= 2 and age < 4:
print("He is learning to walk.")
elif age >= 4 and age <13:
print("He is a child")
elif age >= 13 and age < 20:
print("He is a teenager.")
elif age >= 20 and age < 65:
print("He is an adult.")
else:
print("He is an elder.")
"""5.7 喜欢的水果"""
favorite_fruit = ['watermelon','grapes','banana','cherry']
if 'watermelon' in favorite_fruit:
print("You really like watermelon.")
if 'apple' in favorite_fruit:
print("You really like apple.")
if 'grapes' in favorite_fruit:
print("You really like grapes.")
if 'pear' in favorite_fruit:
print("You really like pear.")
if 'cherry' in favorite_fruit:
print("You really like cherry.")
"""5.8 以特殊方式跟管理员打招呼"""
user_names = ['admin','Bob','Andy','Amy','Mike']
for user in user_names:
if user == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user +", thank you for logging in again")
"""5.9 处理没有用户的情形"""
user_names = ['admin','Bob','Andy','Amy','Mike']
for user in user_names:
if user == 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + user +", thank you for logging in again")
user_names.clear()
if len(user_names) == 0:
print("We need to find some users!")
"""5.10 检查用户名"""
current_users = ['Jackson','Bob','Andy','Amy','Mike']
new_users = ['Bob','Lily','Susan','Andy','David']
for i in range(0,5):
current_users[i] = current_users[i].lower()
for new_user in new_users:
if new_user.lower() in current_users:
print("The name has been used. Please input another name.")
else:
print("The name has not been used.")
"""5.11 序数"""
numbers = ['1','2','3','4','5','6','7','8','9']
for number in numbers:
if number == '1':
print(number + "st")
elif number == '2':
print(number + "nd")
elif number == '3':
print(number + "rd")
else:
print(number + "th")