简体   繁体   English

返回函数值到范围参数步骤

[英]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.

相关问题 更改范围函数内的步长值? - Change step value inside range function? Python-将一个float范围(不带任何步长)作为函数的参数传递 - Python - Pass a range of float (without step) as an argument of a function 用 step 求解一个简单的函数并输出函数的最大值和参数 - Solving a simple function with step and which outputs max value & argument of a function 解决将产生特定返回值的函数的参数 - Solve for the argument to a function that will produce a particular return value python中range()函数的返回值是多少? - What is the return value of the range() function in python? 具有可变步长整数值的range() - range() with variable step integer value 为什么范围 function 考虑给定一个参数作为停止值而不是开始 - Why Range function consider given one argument as stop value and not as start 使用 function 返回值作为关键字参数默认值是否可以接受? - Is it acceptable to use a function return value as a keyword argument default value? 范围 function 的停止值背后的逻辑是什么,python 中的步长值为负 - What's the logic behind stop value of range function with negative step value in python 表达式或函数作为 Range 函数的参数 - Expression or function as an argument of Range function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM