《Python编程-从入门到实践》第五章及第六章习题训练

时间:2023-02-27 14:46:55

5-1条件测试:

pizza = 'cheese'
print("Is pizza == 'cheese'? I predict True.")
print(pizza == 'cheese')
print("Is pizza == 'mushroom'? I predict False.")
print(pizza == 'mushroom')

5-2更多的条件测试:

str1 = 'Happy'
str2 = 'happy'
car = ['Das', 'Toyota', 'Audi']
print(str1 == str2)
print(str1.lower() == str2)
print(1 == 2)
print(1 != 2)
print(1 > 2)
print(1 < 2)
print(1 >= 2)
print(1 <= 2)
print(1 > 2 and 1 > 0)
print(1 > 2 or 1 > 0)
print('Das' in car)
print('BMW' in car)
print('BMW' not in car)
print('Das' not in car)

5-5外星人颜色#3

alien_color = 'green'
if alien_color == 'green':
    print("5 points")
elif alien_color == 'yellow':
    print("10 points")
else:
    print("15 points")

5-10检查用户名:

current_user = ['A', 'B', 'C', 'D', 'E']
new_user = ['a', 'B' , 'F', 'G', 'H']
for i in new_user:
    if i.upper() in current_user:
        print(i + " is already used.")
    else:
        print(i + " is not used")

6-1人:

people = {"first_name" : 'Zhehao', 'last_name' : 'Zhong','age' : 20, 'city':'GuangZhou'}
print(people)

6-5河流:

river = {"nile":'Egypt','Yangtze':'China','Missisipi':'America'}
for k,v in river.items():
    print(k + ' runs through ' + v)
for k,v in river.items():
    print(k)
for k,v in river.items():
    print(v)

6-7人:

people_1 = {"first_name" : 'a', 'last_name' : 'b','age' : 20, 'city':'GuangZhou'}
people_2 = {"first_name" : 'c', 'last_name' : 'd','age' : 20, 'city':'GuangZhou'}
people_3 = {"first_name" : 'e', 'last_name' : 'f','age' : 20, 'city':'GuangZhou'}
people = [people_1, people_2, people_3]
for p in people:
    print(p)

6-11城市:

cities = {
    'gz':{
        'c':1,
        'p':1,
        'f':1
    },
    'sh':{
        'c':2,
        'p':2,
        'f':2
    },
    'bj':{
        'c':3,
        'p':3,
        'f':3
    }
}
for i in cities.items():
    print(i)