函数和 if - else 在 python 中。多重条件。代码学院

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15149667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:29:19  来源:igfitidea点击:

Functions and if - else in python. Mutliple conditions. Codeacademy

pythonfunction

提问by user2121992

Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, we'd use sfor string).

编写一个函数,shut_down,它接受一个参数(您可以使用任何您喜欢的参数;在本例中,我们将s用于字符串)。

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

该shut_down函数返回"Shutting down..."时,它得到"Yes""yes"或者"YES"作为一个参数,而"Shutdown aborted!"当它得到"No""no""NO"。如果它得到除这些输入以外的任何东西,函数应该返回"Sorry, I didn't understand you."

The code I wrote so far is below. It makes errors, e.g. given "No"as the argument, it does not return "Shutdown aborted!"as expected.

我到目前为止编写的代码如下。它会出错,例如"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."

采纳答案by grc

This:

这个:

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

is equivalent to this:

相当于:

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

Which will always return True, since a non-empty string is True.

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

Instead, you want to compare swith each string individually, like so:

相反,您希望s单独与每个字符串进行比较,如下所示:

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

It should end up like this:

它应该像这样结束:

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

回答by Hai Vu

You can do it a couple of ways:

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

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

Or:

或者:

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

回答by xxmbabanexx

Welcome to SO. I am going to walk through the answer, step-by-step.

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

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

This asks if the user would 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."

This is probably new to you. If you have a string, and then .lower()it changes all input from sto lowercase. This is simpler than giving a list of all possibilities.

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

shut_down(s)

This calls the function.

这会调用函数。

回答by eyquem

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's idea is a good one. Probably "yEs" and "yES" etc are acceptable criteria;
Then I propose in this case:

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

回答by Tyler MacDonell

I know this doesn't exactly fit the specification but this is another common option which would catch a few more permutations:

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

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

回答by drewteriyaki

I'm a python programmer and have finished Codecademy. I see that you have a problem and let me give you my answer. It runs perfectly

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

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

回答by SRK635

You can try this code:

你可以试试这个代码:

def shut_down(s):

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

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

回答by Elizabeth Smith

The code from the user 'grc' posted here, almost worked for me. I had to tweak the return message to get it right. If the message (meaning all returned strings) are not exactly the same as described on Codecademy, then the workspace will not validate your response.

来自用户“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"

回答by Isaac Bet

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