我试图用python编写一个使用列表和数字的成员资格测试函数。但它并没有产生预期的结果。我希望得到True或False,但它显示为“None”。任何建议都将不胜感激。谢谢。
下面是我在代码中写的内容:
def bln(x,num):
x = []
num = int()
if (num in x):
return True
num = 4
x = [i for i in range (1,5)]
print(x)
y = bln(x,num)
print(y)
输出:
[1, 2, 3, 4]
None
发布于 2018-06-01 02:57:45
您覆盖了函数参数,因此您没有使用要传递的值。这是正确的方式:
def bln(x,num):
if (num in x):
return True
澄清你在哪里做错了什么。让我们以一个将两个数字相乘的函数为例:
def multiply_numbers(x,y):
return x*y
print(multiply_numbers(3,5)) #15 - all good, numbers are correctly multiplied
def multiply_numbers(x,y):
# x parameter is overwritten here, and will ALWAYS have this values,
#no matter what value you pass when you call the function
x = 5
y = 5
return x*y
print(multiply_numbers(3,5)) #25 - with every input you pass
https://stackoverflow.com/questions/50630947
复制相似问题