Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python函数中包含布尔选项_Python - Fatal编程技术网

如何在python函数中包含布尔选项

如何在python函数中包含布尔选项,python,Python,如何在python函数中包含布尔选项?例如,在panda的库中有一个方法 有一个名为ascending的参数,默认情况下为True,但如果设置为False,则函数将以不同的方式排序。方法的详细信息对我的问题不重要,如果您感兴趣,可以阅读.sort_values()的详细信息: 关键是dataframes有一个默认为true的参数acsending Pandas.DataFrame.sort_值(self、by、axis=0、ascending=True、inplace=False、kind='q

如何在python函数中包含布尔选项?例如,在panda的库中有一个方法 有一个名为ascending的参数,默认情况下为True,但如果设置为False,则函数将以不同的方式排序。方法的详细信息对我的问题不重要,如果您感兴趣,可以阅读.sort_values()的详细信息:

关键是dataframes有一个默认为true的参数acsending Pandas.DataFrame.sort_值(self、by、axis=0、ascending=True、inplace=False、kind='quicksort',na_position='last)

如何使用升序这样的参数编写函数。假设我编写了一个名为make_it_more的函数,它接受一个整数或浮点x,并向其中添加一个。带有一个选项,如果设置为true,将返回x+2。我将如何编写该函数

def make_it_more(x, one_more=False):
    y=x+1
    if one_more==True:
        y=y+1
    return y

我如何编写make_it_more以便它知道只响应一个_more=True,像.sort_values()这样的函数是否检查特定字符串并在给出不匹配字符串时返回错误

使用
,如下所示:

def make_it_more(x, one_more=False):
    amount_to_make_it_more_by = 2 if one_more is True else 1
    return x + amount_to_make_it_more_by
>>> 1 == True
True
>>> 1 is True
False
否则,您会遇到如下问题:

def make_it_more(x, one_more=False):
    amount_to_make_it_more_by = 2 if one_more is True else 1
    return x + amount_to_make_it_more_by
>>> 1 == True
True
>>> 1 is True
False

您当前的代码有什么问题?有错误吗?一:
如果一个以上=真:
应该是
如果一个以上:
。您当前的代码工作正常。我看不出问题出在哪里。我只会写
如果还有一个:
,最终如果有一个是真的:
我只是对.sort\u values()之类的函数感到困惑,在这里有多个“选项”作为参数可以打开或关闭。例如,sort_values(),我很好奇python如何知道您是否打开或关闭了该选项。它完全基于输入参数在字符串中的位置吗?甚至更短的
返回x+(如果一个以上为真,则返回2,否则返回1)
但作为初学者的答案可能不太有用:)