第三周第一次作业

5-1 条件测试

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

print("Is car =='BMW'? I predict Flase")
print(car=='BMW')

print("Is car !='subaru'? I predict False")
print(car!='subaru')

print("Is car !='BMW'? I predict True")
print(car!='BMW')

number=20
print("Is number<30 ? I predict True")
print(number<30)

print("Is number<10 ? I predict False")
print(number<10)

print("Is number in range(10,30) ? I predict True")
print(number<30 and number>10)

str='apple'
print("Is 'a' in str ? I predict True")
print('a' in str)

print("Is 'b' in str ? I predict False")
print('b' in str)

print("Is 'A' in str ? I predict False")
print('A' in str)

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
Is car == 'subaru'? I predict True
True
Is car =='BMW'? I predict Flase
False
Is car !='subaru'? I predict False
False
Is car !='BMW'? I predict True
True
Is number<30 ? I predict True
True
Is number<10 ? I predict False
False
Is number in range(10,30) ? I predict True
True
Is 'a' in str ? I predict True
True
Is 'b' in str ? I predict False
False
Is 'A' in str ? I predict False
False

Process finished with exit code 0

5-3  外星人颜色#1

alien_color='blue'
if alien_color=='green':
    print("Congratulation!You have got 5 points!")
else:
    print()
    
alien_color='green'
if alien_color=='green':
    print("Congratulation!You have got 5 points!")
else:
    print()

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test

Congratulation!You have got 5 points!

Process finished with exit code 0

5-4 外星人颜色#2

def test(str):
    if(str=='green'):
        print('You have got 5 points!')
    else:
        print('You have got 10 points!')

first_alien='green'
second_alien='blue'

test(first_alien)
test(second_alien)

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
You have got 5 points!
You have got 10 points!

Process finished with exit code 0

5-5 外星人颜色#3

def test(str):
    if(str=='green'):
        print('You have got 5 points!')
    elif(str=='yellow'):
        print('You have got 10 points!')
    elif (str == 'red'):
        print('You have got 15 points!')



green_alien='green'
yellow_alien='yellow'
red_alien='red'

test(green_alien)
test(yellow_alien)
test(red_alien)

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
You have got 5 points!
You have got 10 points!
You have got 15 points!

Process finished with exit code 0

5-6 人生的不同阶段

def age_period(age):
    if age<2:
        print('He is a baby!')
    elif age<4:
        print('He is a toddler!')
    elif age<13:
        print('He is a child!')
    elif age<20:
        print('He is a teenage!')
    elif age<65:
        print('He is an adult!')
    else:
        print('He is an older')

age=[1,3,5,17,33,70]
for x in age:
    age_period(x)

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
He is a baby!
He is a toddler!
He is a child!
He is a teenage!
He is an adult!
He is an older

Process finished with exit code 0

5-7 喜欢的水果

favorite_fruits=['apples','bananas','pears']

fruit=['apples','pears','oranges','bananas','grapefruit']
for x in fruit:
    if x in favorite_fruits:
        print("You really like",x);

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
You really like apples
You really like pears
You really like bananas

Process finished with exit code 0

5-8 以特殊的方式跟管理员打招呼

user_name=['Chan','Tan','Li','Hu','admin']
for x in user_name:
    if x == 'admin':
        print('Hello admin,would you like to see a status report?')
    else:
        print('Hello',x,',thank you for logging in again')

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
Hello Chan ,thank you for logging in again
Hello Tan ,thank you for logging in again
Hello Li ,thank you for logging in again
Hello Hu ,thank you for logging in again
Hello admin,would you like to see a status report?

Process finished with exit code 0

5-9 处理没有用户的情形

user_name=[]
if user_name:
    for x in user_name:
        if x == 'admin':
            print('Hello admin,would you like to see a status report?')
        else:
            print('Hello',x,',thank you for logging in again')
else:
    print('We need to find some users!')

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
We need to find some users!

Process finished with exit code 0

5-10 检查用户名

def check(name):
    current_users=['chan','tan','li','lin','wang','hu'];
    if name.lower() in current_users:
        print('The name ',name,' is occupied !')
    else:
        print("The name ",name," isn't occupied!")

new_users=['LiN','Tan','Alice','JustIn','JOHn']
for name in new_users:
    check(name)

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
The name  LiN  is occupied !
The name  Tan  is occupied !
The name  Alice  isn't occupied!
The name  JustIn  isn't occupied!
The name  JOHn  isn't occupied!

Process finished with exit code 0

5-11 序数

def print_order(num):
    if num == 1:
        print('1st')
    elif num == 2:
        print('2nd')
    elif num == 3:
        print('3rd')
    else:
        print(num,'th')

for x in range(1,10):
    print_order(x)

Output:

/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
1st
2nd
3rd
4 th
5 th
6 th
7 th
8 th
9 th

Process finished with exit code 0






















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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值