V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
char1998
V2EX  ›  Python

一个关于 Python 的函数问题,求大佬指导

  •  
  •   char1998 · 2017-06-02 13:51:13 +08:00 · 3509 次点击
    这是一个创建于 2491 天前的主题,其中的信息可能已经有所发展或是发生改变。

    First, def a function, shut_down, that takes one arguments. Don't forget the parentheses or the colon!

    Then, if the shut_down function receives an s equal to "yes", it should return "Shutting down"

    Alternatively, elif s is equal to "no", then the function should return "Shutdown aborted".

    Finally, if shut_down gets anything other than those inputs, the function should return "Sorry"

    谷歌的翻译如下:

    首先,def一个函数,shut_down它需要一个参数 s。 不要忘记括号或冒号! 然后,如果shut_down函数接收到等于“ yes ”的 s,则应返回“ Shutting down ” 或者,elif等于"no",那么函数应该返回"Shutdown aborted"。 最后,如果shut_down得到除了这些输入之外的任何东西,该函数应该返回"Sorry"

    def shut_down(s):
        return s
    if __:
        return "Shutting down"
    elif ___:
        return "Shutdown aborted"
    else:
        return "Sorry"
    

    试过几种。但是好像不行,求指导

    davinci
        1
    davinci  
       2017-06-02 13:59:58 +08:00
    你能用 python 和 flask 搭建 bbs 论坛 https://www.v2ex.com/t/361795#reply27,却连一个简单的 python 函数都不会写?
    char1998
        2
    char1998  
    OP
       2017-06-02 14:03:39 +08:00
    那个论坛,没多少 python 的基础语法,我就会用那个 flask 框架,所以 python 的基础没学好
    char1998
        3
    char1998  
    OP
       2017-06-02 14:04:29 +08:00
    @davinci 所有工作没回音,现在在从头学基础
    davinci
        4
    davinci  
       2017-06-02 14:05:43 +08:00
    @char1998 从你贴出来的代码来看。你连 if 语句都不会用,根本不会编程。就好比学数学确不会算加减乘除法。
    thekoc
        5
    thekoc  
       2017-06-02 14:06:21 +08:00
    @char1998 这个不是基础没学好了……这只是单纯的不会 Python 而已
    char1998
        6
    char1998  
    OP
       2017-06-02 14:08:40 +08:00
    @davinci 你是指下划线 ? 那个下划线是条件判断
    char1998
        7
    char1998  
    OP
       2017-06-02 14:10:47 +08:00
    @thekoc 好吧,我还是想问问这个条件应该怎么写才能通过
    rocksolid
        8
    rocksolid  
       2017-06-02 14:13:47 +08:00
    1, s = 'yes'
    2, s = 'no'
    ......
    rocksolid
        9
    rocksolid  
       2017-06-02 14:14:12 +08:00
    我也醉了。。。
    1, s == 'yes'
    2, s == 'no'
    ......
    SuT2i
        10
    SuT2i  
       2017-06-02 14:14:14 +08:00
    原题是这样吗??
    jy02201949
        11
    jy02201949  
       2017-06-02 14:16:06 +08:00
    没缩进,看着眼睛疼
    huamiao
        12
    huamiao  
       2017-06-02 14:16:18 +08:00
    @thekoc 我想他说的意思是,你贴出的代码函数定义第一句就是 return,那后面的代码都没有必要了,因为根本不会执行。这种问题放在任何语言上都是问题。
    SuT2i
        13
    SuT2i  
       2017-06-02 14:18:38 +08:00
    def shut_down(s):
    return s

    def func():
    if shut_down()=='yes':
    return 'Shutting down'
    elif shut_down()=='no':
    return 'Shutdown aborted'
    else:
    return 'Sorry'
    SuT2i
        14
    SuT2i  
       2017-06-02 14:23:30 +08:00
    SuT2i
        15
    SuT2i  
       2017-06-02 14:24:03 +08:00
    @SuT2i 我去。。没有缩进好难受。。。。
    huamiao
        16
    huamiao  
       2017-06-02 14:25:05 +08:00
    @thekoc 抱歉,我本身不写 python,查了一下 python 的语法,没想到 python 是用缩进控制代码块的。
    char1998
        17
    char1998  
    OP
       2017-06-02 14:26:47 +08:00
    @SuT2i 这就是原题。。shut_down 函数接收到的 s 为 yes 就 return "Shutting down"
    char1998
        18
    char1998  
    OP
       2017-06-02 14:27:12 +08:00
    @huamiao 但是这个就是原题。。我也没弄清楚
    char1998
        19
    char1998  
    OP
       2017-06-02 14:30:08 +08:00
    @SuT2i 这种的我之前就试过了,之前就试过好多 ,都不行
    char1998
        20
    char1998  
    OP
       2017-06-02 14:31:23 +08:00
    @rocksolid 这种不行。。。这种早就试过了
    char1998
        21
    char1998  
    OP
       2017-06-02 14:32:43 +08:00
    @rocksolid 这么写 return 根本不能返回
    huamiao
        22
    huamiao  
       2017-06-02 14:32:54 +08:00
    @char1998 你的意思是说代码段也是题目的一部分?题目要求在横线处填空?
    char1998
        23
    char1998  
    OP
       2017-06-02 14:37:24 +08:00
    @huamiao 不是,代码是我自己写的,横线初填空是我感觉,题目说返回那个,我就直接返回了,然后就是我感觉判断语句写了好多遍都是错了
    huamiao
        24
    huamiao  
       2017-06-02 14:39:04 +08:00
    如果你说的是 https://www.codecademy.com/courses/python-beginner-c7VZg/4/1
    我试着写了以下代码是可以工作的。
    def shut_down(s):
    if s == 'yes':
    return 'Shutting down'
    elif s == 'no':
    return 'Shutdown aborted'
    else:
    return 'Sorry'

    另,python 感觉很方便的样子,决定学一下。
    huamiao
        25
    huamiao  
       2017-06-02 14:42:24 +08:00
    因为左边的要求是分 1,2,3,4 步的,第一步的要求是定义一个 shut_down 函数,所以不应该把其他的代码都写在函数外面,这样与第一个要求不符。而我猜测后台判断应该是用相当于单元测试的方法去执行你写入的代码块的。
    wuqiangroy
        26
    wuqiangroy  
       2017-06-02 15:03:10 +08:00
    def shut_down(s):
    if s == "yes":
    return "Shutting down"
    elif s == "no":
    return "shutdown aborted"
    else:
    return "Sorry"
    …………………………………………………………………………
    LZ 你用 flask 写了个那么漂亮的 blog,这个不会???
    ipwx
        27
    ipwx  
       2017-06-02 15:07:23 +08:00
    return {'yes': 'Shutting down', 'no': 'Shutdown aborted'}.get(s, 'Sorry')
    Kilerd
        28
    Kilerd  
       2017-06-02 15:20:05 +08:00   ❤️ 1
    shut_down = lambda x: {"yes": "Shutting down", "no": "shutdown aborted"}.get(x, "Sorry")
    scriptB0y
        29
    scriptB0y  
       2017-06-02 15:24:52 +08:00
    你们在干嘛?
    char1998
        30
    char1998  
    OP
       2017-06-02 15:32:35 +08:00
    @huamiao 确实,有时候脑子没转过弯来,很感谢提醒
    @wuqiangroy 说出来自己都不信,但是事实就是这样,仅仅会用 flask 而已,而且那个 blog,前端偏多,然后就是 python 基础语法用到很少
    @ipwx 大佬,要按照那种规则来,这种这种应该是最简单的字典返回了吧
    @Kilerd lambda,可以,这很 Pythonic
    @scriptB0y 不知道
    Kilerd
        31
    Kilerd  
       2017-06-02 15:36:28 +08:00
    @char1998 # 27 写得没问题。

    我写的是 lambda 形式而已

    她的意思是这样的

    def shut_down(s):
    ····return {'yes': 'Shutting down', 'no': 'Shutdown aborted'}.get(s, 'Sorry')
    char1998
        32
    char1998  
    OP
       2017-06-02 15:42:29 +08:00
    @Kilerd return 这种返回数据我就用来写过`json`,今天涨知识了,我想问问`.get`的效果能解释下吗
    ty89
        33
    ty89  
       2017-06-02 15:46:13 +08:00
    @Kilerd 这很 pythonic
    HMSQQbA
        34
    HMSQQbA  
       2017-06-02 15:53:33 +08:00 via Android
    莫名其妙的问题。
    Kilerd
        35
    Kilerd  
       2017-06-02 15:58:46 +08:00
    TuringGunner
        36
    TuringGunner  
       2017-06-03 19:18:47 +08:00
    惭愧惭愧,只会一般的写法,这种高端的写法,厉害
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1176 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:27 · PVG 02:27 · LAX 11:27 · JFK 14:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.