codecademy.com关于python的两道小程序

题目一:

Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, we'd use s for string). The shut_down function should return "Shutting down..." when it gets "Yes", "yes", or "YES" as an argument, and "Shutdown aborted!" when it gets "No", "no", or "NO".

If it gets anything other than those inputs, the function should return "Sorry, I didn't understand you."

代码如下:

def shut_down(s):
    if s.lower()=="yes":
        return "Shutting down..."
    elif s.lower()=="no":
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."

或者

def shut_down(s):
    if s=="YES" or s=="Yes" or s=="yes":
        return "Shutting down..."
    elif s=="NO" or s=="no" or s=="No":
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."
或者

def shut_down(s):
    if s in ["YES","Yes","yes"]:
        return "Shutting down..."
    elif s in ["NO","no","No"]:
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."


============================

题目二:

This is a two-parter: first, define a function, distance_from_zero, with one parameter (choose any parameter name you like).

Second, have that function do the following:

Check the type of the input it receives.
If the type is int or float, the function should return the absolute value of the function input.
If the type is any other type, the function should return "Not an integer or float!"


代码如下:

def distance_from_zero(s):
    if type(s)==int or type(s)==float:
        return abs(s)
    else:
        return "Not an integer or float!"



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值