6

编写一个函数,shut_down,它接受一个参数(你可以使用任何你喜欢的东西;在这种情况下,我们将使用s字符串)。

shutdown_down 函数应"Shutting down..."在获得"Yes""yes""YES"作为参数时以及"Shutdown aborted!"获得"No""no"或时返回"NO"。如果它得到的不是这些输入,函数应该返回"Sorry, I didn't understand you."

到目前为止我写的代码如下。它会出错,例如"No"作为参数给出,它不会"Shutdown aborted!"按预期返回。

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

9 回答 9

11

这:

s == "Yes" or "yes" or "YES"

相当于:

(s == "Yes") or ("yes") or ("YES")

它将始终返回True,因为非空字符串是True

相反,您想分别s与每个字符串进行比较,如下所示:

(s == "Yes") or (s == "yes") or (s == "YES")  # brackets just for clarification

它最终应该是这样的:

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."
于 2013-03-01T02:53:32.347 回答
5

您可以通过以下几种方式做到这一点:

if s == 'Yes' or s == 'yes' or s == 'YES':
    return "Shutting down..."

或者:

if s in ['Yes', 'yes', 'YES']:
    return "Shutting down..."
于 2013-03-01T02:55:01.693 回答
2

欢迎来到 SO。我将逐步完成答案。

s = raw_input ("Would you like to shut down?")

这会询问用户是否要关闭。

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

这对你来说可能是新的。如果您有一个字符串,然后.lower()它将所有输入从小s写更改。这比列出所有可能性要简单。

shut_down(s)

这调用了函数。

于 2013-03-01T02:57:12.790 回答
2
def shut_down(s):
    return ("Shutting down..." if s in("Yes","yes","YES")
            else "Shutdown aborted!" if s in ("No","no","NO")
            else "Sorry, I didn't understand you.")

GordonsBeard 的想法很好。可能“yEs”和“yES”等是可接受的标准;
然后我在这种情况下建议:

def shut_down(s,d = {'yes':"Shutting down...",'no':"Shutdown aborted!"}):
    return d.get(s.lower(),"Sorry, I didn't understand you.")
于 2013-03-01T02:58:12.887 回答
1

我知道这并不完全符合规范,但这是另一个常见的选项,可以捕捉更多的排列:

def shut_down(s):
    s = s.upper()
    if s == "YES":
        return "Shutting down..."
    elif s == "NO":
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."
于 2013-03-01T02:58:36.587 回答
1
def shut_down(phrase):
    word = phrase
    return word

take_action = input(shut_down('do you want to shutdown the program?: '.title()))
if take_action.lower() == 'yes':
    print('Shutting down...')
elif take_action.lower() == 'no':
    print('Shutdown aborted!')
else:
    print('Sorry, I didn\'t understand you.')
于 2019-02-15T12:35:24.243 回答
0

我是一名 Python 程序员,并且已经完成了 Codecademy。我看到你有问题,让我给你我的答案。它运行完美

def shut_down(s):
    if s == "yes":
        return "Shutting down"
    elif s == "no":
        return "Shutdown aborted"
    else:
        return "Sorry"
于 2016-03-19T00:14:03.970 回答
0

你可以试试这段代码:

def shut_down(s):

if s =="yes":
    return "Shutting Down"

elif s =="no":
    return "Shutdown aborted"
else:
    return "Sorry"
print shut_down("yes")   
于 2017-02-15T15:11:27.437 回答
0

此处发布的用户“grc”的代码几乎对我有用。我不得不调整返回消息以使其正确。如果消息(意味着所有返回的字符串)与 Codecademy 中描述的不完全相同,则工作区将不会验证您的响应。

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"
于 2018-06-15T16:35:24.983 回答