函数和if - else在python中。 Codecademy网站

时间:2013-03-01 02:51:34

标签: python function

写一个函数,shut_down,它接受一个参数(你可以使用你喜欢的任何东西;在这种情况下,我们使用s作为字符串)。当shut_down函数获得“是”,“是”或“是”作为参数时,应该返回“关闭...”,并且“关闭已中止!”当它变为“否”,“否”或“否”时。

如果它得到的不是那些输入,该函数应该返回“抱歉,我不明白你。”

这是我的代码到目前为止......它的错误并说“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."

9 个答案:

答案 0 :(得分: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."

答案 1 :(得分:5)

你可以通过以下两种方式实现:

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

或者:

if s in ['Yes', 'yes', 'YES']:
    return "Shutting down..."

答案 2 :(得分: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)

这会调用该函数。

答案 3 :(得分: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.")

答案 4 :(得分:0)

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

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."

答案 5 :(得分:0)

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

def shut_down(s):
    if s == "yes":
        return "Shutting down"
    elif s == "no":
        return "Shutdown aborted"
    else:
        return "Sorry"

答案 6 :(得分:0)

您可以尝试以下代码:

def shut_down(s):

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

elif s =="no":
    return "Shutdown aborted"
else:
    return "Sorry"
print shut_down("yes")   

答案 7 :(得分: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"

答案 8 :(得分:0)

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.')