[英]Return function value to range argument step
Hi can some one point me some guidance, i pretend to pass the value of function schema to a variable that will be used as the argument step inside the range module. 嗨,有人可以给我一些指导吗,我假装将函数架构的值传递给一个变量,该变量将用作范围模块内的参数步骤。 i hope its clear .. sorry the newbie guys. 我希望清楚..对不起,新手们。 after some trials i got the edit fine. 经过一些试验,我得到了很好的编辑。
import os
import sys
def schema(x):
if x == 'M':
step=1
else:
step=2
return step
def main():
left_start = 1
left_end = 9
sch = schema('M')
range1= range(left_start,left_end,sch)
if 2 in range1:
print "In range"
else:
print "Missing in range"
if __name__ == '__main__':
main()
Your code has a problem with indentation. 您的代码有缩进问题。 The following: 下列:
def schema(x):
if x == 'M':
step=1
else:
step=2
return step
should read 应该读
def schema(x):
if x == 'M':
step=1
else:
step=2
return step
Otherwise, the function returns None
when x == 'M'
. 否则,当x == 'M'
时,该函数返回None
。
A more concise way to write that function is: 编写该函数的一种更简洁的方法是:
def schema(x):
return 1 if x == 'M' else 2
It looks like you want to do the following: 您似乎要执行以下操作:
def schema(x):
if x == 'M':
step=1
else:
step=2
return step
Notice how the return step
is indented to the same level as the if
statement. 请注意, return step
如何缩进到与if
语句相同的级别。 In your code, the return step
will only run in the else
case. 在您的代码中, return step
仅在else
情况下运行。 In the case where x == 'M'
, your code would return None
from schema()
. 在x == 'M'
的情况下,您的代码将从schema()
返回None
。
My suggested code will run the return step
in either case. 无论哪种情况,我建议的代码都将运行return step
。
The problem in indentation your code: 缩进代码的问题:
def schema(x):
if x == 'M':
step=1
else:
step=2
return step
it should as: 它应为:
def schema(x):
if x == 'M':
step=1
else:
step=2
return step
in your code the step is returned IF and only if x!='M'. 在您的代码中,仅当x!='M'时才返回该步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.