Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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,简单的修复,但我如何才能使这停止循环?结果是,它只是重复地请求值。还有一种方法,我可以检查所有的值,以确保在20到40之间 def main(): print("Welcome to the autostacker") print(getDim()) def getDim(): height = int(input("Enter the height: ")) width = int(input("Enter the width: ")) len

简单的修复,但我如何才能使这停止循环?结果是,它只是重复地请求值。还有一种方法,我可以检查所有的值,以确保在20到40之间

def main():    
    print("Welcome to the autostacker")
    print(getDim())

def getDim():
    height = int(input("Enter the height: "))
    width = int(input("Enter the width: "))
    length = int(input("Enter the width: "))
    return getDim()


main()
我想,getDim中的return语句是循环的罪魁祸首。您可以检查这些值是否在如下所示的范围内

def main():    
    print("Welcome to the autostacker")
    print(getDim())

def getDim():
    height = int(input("Enter the height: "))
    width = int(input("Enter the width: "))
    length = int(input("Enter the width: "))
    if 20 <= height <=40 and 20 <= width <= 40 and 20 <= length <= 40:
         print "all the values lie in the range"
         # DO WHAT YOU WANT
    else:
        print " values are not in range"
        # DO SOMETHING ELSE
    return height, width,length

main()
差不多

def main():    
    print("Welcome to the autostacker")
    h, w, l = getDim()

    if 20 <= h <= 40:
        if 20 <= w <= 40:
            if 20 <= l <= 40:
                print "height is %d width is %d length is %d" % (h, w, l)
            else:
                print 'length out of range'
        else:
            print 'width out of range'
    else:
        print 'height out of range'




def getDim():
    height = int(input("Enter the height: "))
    width = int(input("Enter the width: "))
    length = int(input("Enter the length: "))
    return height, width, length

main()

确保不要在getDim中调用getDim,除非您有某种条件最终使其停止调用本身。

将return getDim更改为返回高度、重量、长度如何显示正在输入的字段?您当前拥有的是一个永无止境的递归循环。[20