Python学习——if语句

if语句

思维导图

在这里插入图片描述

代码

# 5-1
cars = ['bmw', 'audi', 'subaru', 'toyota']
print(cars[2] == 'subaru')
print(cars[3] == 'subaru')
print(cars[2] != 'subaru')
print(cars[3] != 'subaru')
print(cars[2] == 'subaru' and cars[3] != 'toyota')
print(cars[2] == 'subaru' or cars[3] == 'subaru')
print('bmw' in cars)
print('subaru' not in cars)
age = 22
print(age == 21)
print(age != 21)
# 5-2
print("5-2")
str1 = 'Bmw'
# print(str1 == cars[0])
# print(str1 != cars[0])
# print(str1.lower() == cars[0].lower())
# print(str1.lower() != cars[0].lower())
numbers = list(range(1, 5))
# print(numbers[1] == numbers[2])
# print(numbers[1] != numbers[2])
# print(numbers[1] > numbers[2])
# print(numbers[1] < numbers[2])
# print(numbers[1] <= numbers[2])
# print(numbers[1] >= numbers[2])
print(cars[0] == 'bmw' and cars[3] == 'subaru')
print(cars[0] == 'bmw' or cars[3] == 'subaru')
print(9 in numbers)
print(9 not in numbers)
# 5-3
print("5-3")
alien_color = 'red'
if alien_color == 'green':
    print("You got 5 points.")
# 5-4
print("5-4")
if alien_color == 'green':
    print("You got 5 points.")
else:
    print("You got 10 points.")
# 5-5
print("5-5")
if alien_color == 'green':
    print("You got 5 points.")
elif alien_color == 'yellow':
    print("You got 10 points.")
else:
    print("You got 15 points.")
# 5-6
print("5-6")
age = 65
if age < 2:
    print("It is a baby.")
elif age < 4:
    print("It is a toddler.")
elif age < 13:
    print("It is a children.")
elif age < 20:
    print("It is a teenagers.")
elif age < 65:
    print("It is a adult.")
else:
    print("It is a old people.")
# 5-7
favorite_fruits = ['apple', 'banana', 'orange']
if 'apple' in favorite_fruits:
    print("You really like apple!")
if 'banana' in favorite_fruits:
    print("You really like banana!")
if 'orange' in favorite_fruits:
    print("You really like orange!")
if 'pear' in favorite_fruits:
    print("You really like pear!")
if 'peach' in favorite_fruits:
    print("You really like peach!")
# 5-8
print("5-8")
user_names = ['admin', 'root', 'scott', 'hr', 'system']
for user_name in user_names:
    if user_name == 'admin':
        print("Hello admin,would you like to see a status report?")
    else:
        print("Hello " + user_name + ",thank you for logging in again")
# 5-9
print("5-9")
user_names = []
if user_names:
    for user_name in user_names:
        if user_name == 'admin':
            print("Hello admin,would you like to see a status report?")
        else:
            print("Hello " + user_name + ",thank you for logging in again")
else:
    print("We need to find some users!")
# 5-10
print("5-10")
current_users = ['admin', 'root', 'scott', 'hr', 'John']
new_users = ['admin', 'root', 'Python', 'hzx', 'JOHN']
current_users_copy =[]
for current_user in current_users:
    current_users_copy.append(current_user.lower())
print("It's all done.")
for new_user in new_users:
    if new_user.lower() in current_users_copy:
        print("You need to enter a different user name.")
    else:
        print("This user name is not used")
# 5-11
print("5-11")
ordinal_numbers = list(range(1, 10))
for ordinal_number in ordinal_numbers:
    if ordinal_number == 1:
        print("1st")
    elif ordinal_number == 2:
        print("2nd")
    elif ordinal_number == 3:
        print("3rd")
    elif ordinal_number == 4:
        print("4th")
    elif ordinal_number == 5:
        print("5th")
    elif ordinal_number == 6:
        print("6th")
    elif ordinal_number == 7:
        print("7th")
    elif ordinal_number == 8:
        print("8th")
    else:
        print("9th")

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:白松林 返回首页