python第五章练习5-1~5-7

练习可能不全,仅作记录,欢迎指正

5-1 条件测试 : 编写一系列条件测试; 将每个测试以及你对其结果的预测和实际结果都打印出来。 你编写的代码应类似于下面这样:

print("Is car=='subaru'?I predict True.")
print(car=='subaru')

print("\nIs car=='audi'?I predict False.")
print(car=='audi')


candy='sweet'
print("\nIs candy=='sweet'?I predict True.")
print(candy=='sweet')

print("\nIs candy=='mild'?I predict False.")
print(candy=='mild')

5-2 更多的条件测试 : 你并非只能创建10个测试。 如果你想尝试做更多的比较, 可再编写一些测试, 并将它们加入到conditional_tests.py中。 对于下面列出的各种测试, 至少编写一个结果为True 和False 的测试。

candy='Sweet'
candy2='sweet'
print(candy+'=='+candy2,candy==candy2)
print(candy.lower()+'=='+candy2.lower(),candy.lower()==candy2.lower())

print(18==17)
print(18!=17)
print(18>17)
print(18<17)
print(18>=17)
print(18<=17)

print('\n')
print(18>17 and 18<19)

cars=['audi','benz','bmw']
car='benz'
car2='toyota'
if car in cars:
    print('Tru')
if car2 not in cars:
    print('123')

5-3 外星人颜色#1

alien_color='green'
if alien_color=='green':
    print('U got 5 point.')

alien_color1='yellow'
if alien_color1=='green':
    print('U got 5 point.')


alien1='green'
alien2='red'
if alien1=='green':
    print('U got 5 point.')

if alien2!='green':
    print('U got 10 point.')

if alien1=='green':
    print('U got 6 point.')
else:
    print('U got 13 point.')

5-5 外星人颜色#3
 

alien1='green'
if alien1=='green':
    print('u got 5 point.')
elif alien1=='yellow':
    print('u got 10 point.')
else:
    print('u got 15 point.')

alien2='yellow'
if alien2=='green':
    print('u got 5 point.')
elif alien2=='yellow':
    print('u got 10 point.')
else:
    print('u got 15 point.')

alien3='red'
if alien3=='green':
    print('u got 5 point.')
elif alien3=='yellow':
    print('u got 10 point.')
else:
    print('u got 15 point.')

5-6 人生的不同阶段

age=13
if age<2:
    print('He is a baby.')
elif age<4:
    print('He is learning to walk.')
elif age<13:
    print('He is a child.')
elif age<20:
    print('He is a teenager.')
elif age<65:
    print('He is a adult.')
else:
    print('He is a old man.')

5-7 喜欢的水果
 

fruits=['apple','orange','banana']
if 'apple' in fruits:
    print('U really like apples.')
if 'bear' in fruits:
    print('U really like bears.')
if 'banana' in fruits:
    print('U really like bananas.')

 

你可能感兴趣的:(学习之路)