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

2017-06-02 13:51:13 +08:00
 char1998

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"

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

3553 次点击
所在节点    Python
36 条回复
davinci
2017-06-02 13:59:58 +08:00
你能用 python 和 flask 搭建 bbs 论坛 https://www.v2ex.com/t/361795#reply27,却连一个简单的 python 函数都不会写?
char1998
2017-06-02 14:03:39 +08:00
那个论坛,没多少 python 的基础语法,我就会用那个 flask 框架,所以 python 的基础没学好
char1998
2017-06-02 14:04:29 +08:00
@davinci 所有工作没回音,现在在从头学基础
davinci
2017-06-02 14:05:43 +08:00
@char1998 从你贴出来的代码来看。你连 if 语句都不会用,根本不会编程。就好比学数学确不会算加减乘除法。
thekoc
2017-06-02 14:06:21 +08:00
@char1998 这个不是基础没学好了……这只是单纯的不会 Python 而已
char1998
2017-06-02 14:08:40 +08:00
@davinci 你是指下划线 ? 那个下划线是条件判断
char1998
2017-06-02 14:10:47 +08:00
@thekoc 好吧,我还是想问问这个条件应该怎么写才能通过
rocksolid
2017-06-02 14:13:47 +08:00
1, s = 'yes'
2, s = 'no'
......
rocksolid
2017-06-02 14:14:12 +08:00
我也醉了。。。
1, s == 'yes'
2, s == 'no'
......
SuT2i
2017-06-02 14:14:14 +08:00
原题是这样吗??
jy02201949
2017-06-02 14:16:06 +08:00
没缩进,看着眼睛疼
huamiao
2017-06-02 14:16:18 +08:00
@thekoc 我想他说的意思是,你贴出的代码函数定义第一句就是 return,那后面的代码都没有必要了,因为根本不会执行。这种问题放在任何语言上都是问题。
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
2017-06-02 14:23:30 +08:00
SuT2i
2017-06-02 14:24:03 +08:00
@SuT2i 我去。。没有缩进好难受。。。。
huamiao
2017-06-02 14:25:05 +08:00
@thekoc 抱歉,我本身不写 python,查了一下 python 的语法,没想到 python 是用缩进控制代码块的。
char1998
2017-06-02 14:26:47 +08:00
@SuT2i 这就是原题。。shut_down 函数接收到的 s 为 yes 就 return "Shutting down"
char1998
2017-06-02 14:27:12 +08:00
@huamiao 但是这个就是原题。。我也没弄清楚
char1998
2017-06-02 14:30:08 +08:00
@SuT2i 这种的我之前就试过了,之前就试过好多 ,都不行
char1998
2017-06-02 14:31:23 +08:00
@rocksolid 这种不行。。。这种早就试过了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/365436

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX