some answer for chapter 5

5-2

name = 'Lin'

print("Is name =='Lin'? I predict True.")

print(name =='Lin')

print("\nIs name== 'Ling'?I predict False.")

print(name =='Ling')

print("\nIsname.lower() == 'lin'? I predict True.")

print(name.lower() =='lin')

print("\nIsname.lower() == 'ling'?I predict False.")

print(name.lower() =='ling')

print("\nIs 5+2== 7 ? I predict True.")

print(5+2==7)

print("\nIs 5+2!= 7 ? I predict False.")

print(5+2!=7)

print("\nIs 5< 7 ? I predict True.")

print(5<7)

print("\nIs 5> 7 ? I predict False.")

print(5>7)

print("\nIs 5<= 7 ? I predict True.")

print(5<=7)

print("\nIs 5>= 7 ? I predict False.")

print(5>=7)

print("\nIs 7> 5 and 7 < 9 ? I predict True.")

print( 7>5and7<9 )

print("\nIs 7< 5 or 7 > 9 ? I predict False.")

print( 7<5or7>9 )

lists = ['a','b','c']

print("\nIs a inthe lists ? I predict True.")

print('a'in lists)

print("\nIs d inthe lists ? I predict False.")

print('d'in lists)

print("\nIs a notin the lists ? I predict True.")

print('d'not in lists)

print("\nIs a notin the lists ? I predict False.")

print('a'not in lists)

output:

Is name == 'Lin'? I predict True.

True

 

Is name == 'Ling'?I predict False.

False

 

Is name.lower() =='lin'? I predict True.

True

 

Is name.lower() == 'ling'?I predict False.

False

 

Is 5+2 == 7 ? I predict True.

True

 

Is 5+2 != 7 ? I predict False.

False

 

Is 5 < 7 ? I predict True.

True

 

Is 5 > 7 ? I predict False.

False

 

Is 5 <= 7 ? I predict True.

True

 

Is 5 >= 7 ? I predict False.

False

 

Is 7 > 5 and 7< 9 ? I predict True.

True

 

Is 7 < 5 or 7 >9 ? I predict False.

False

 

Is a in the lists ? I predict True.

True

 

Is d in the lists ? I predict False.

False

 

Is a not in the lists? I predict True.

True

 

Is a not in the lists? I predict False.

False

5-3

color = ['green','yellow','red']

print("test1:")

alien_color = color[0]

if alien_color =='green':

    print('you get 5points')

print("test2:")

alien_color = color[1]

if alien_color =='green':

    print('you get 5points')

output:

test1:

you get 5 points

test2:

5-4

print("test1:")

alien_color = color[0]

if alien_color =='green':

    print('you get 5points')

else:

    print('you get 10points')

print("test2:")

alien_color = color[1]

if alien_color =='green':

    print('you get 5points')

else:

    print('you get 10points')

test1:

you get 5 points

test2:

you get 10 points

5-5

print("test1:")

alien_color = color[0]

if alien_color =='green':

    print('you get 5points')

elif alien_color =='yellow':

    print('you get 10points')

else:

    print('you get 15 points')

print("test2:")

alien_color = color[1]

if alien_color =='green':

    print('you get 5points')

elif alien_color =='yellow':

    print('you get 10points')

else:

    print('you get 15points')

print("test3:")

alien_color = color[2]

if alien_color =='green':

    print('you get 5points')

elif alien_color =='yellow':

    print('you get 10points')

else:

    print('you get 15points')

test1:

you get 5 points

test2:

you get 10 points

test3:

you get 15 points

5-6

age = 20

if age <2 :

    print("He is ababy.")

elif age <4 :

    print("He islearning to walk.")

elif age <13 :

    print("He is achild.")

elif age <20 :

    print("He is ateenager.")

elif age <65 :

    print("He is anadult.")

else:

    print("He is anelder.")

output:

He is an adult.

5-7

favorite_fruits = ['apple','banana','orange']

tests = ['apple','lemon','pear','banana','orange']

if tests[0] in favorite_fruits:

    print("you reallylike "+ tests[0] +"!")

if tests[1] in favorite_fruits:

    print("you reallylike "+ tests[1] +"!")

if tests[2] in favorite_fruits:

    print("you reallylike "+ tests[2] +"!")

if tests[3] in favorite_fruits:

    print("you reallylike "+ tests[3] +"!")

if tests[4] in favorite_fruits:

    print("you reallylike "+ tests[4] +"!")

you really like apple!

you really like banana!

you really like orange!

5-8 5-9

users = ['admin','b','c','d','e']

for user in users:

    if user =='admin':

       print("Hello admin,would you like to see a status report?")

    else:

       print("Hello "+ user +",thank youfor logging in again.")

if len(users)==0 :

    print('We need to findsome users!')

del users[:]

if len(users)==0 :

    print('We need to findsome users!')

Hello admin,would you like to see a statusreport?

Hello b,thank you for logging in again.

Hello c,thank you for logging in again.

Hello d,thank you for logging in again.

Hello e,thank you for logging in again.

We need to find some users!

5-10

current_users = ['a','b','c','D','e']

new_users = ['a','d','g','j','m']

for new_user in new_users:

   flag = False;

    for current_user in current_users:

       if new_user.lower()==current_user.lower():

           flag = True

           break

    if flag:

       print("the name "+ new_user +" isoccupied, you need to input another one!")

    else:

       print("the name "+ new_user +" have notbeen used.")

output:

the name a is occupied, you need to inputanother one!

the name d is occupied, you need to inputanother one!

the name g have not been used.

the name j have not been used.

the name m have not been used.

5-11

list=[i for i inrange(1,10)]

for i inlist:

    if i ==1:

       print(str(i)+"st")

    elif i ==2:

       print(str(i)+'rd')

    else:

       print(str(i)+"th")

output:

1st

2rd

3th

4th

5th

6th

7th

8th

9th

 


 
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值