Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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新手,正在做这个家庭作业。我需要创建一个带有菜单的小程序。直到现在我还很好。我有点迷路了。如何将值传递给函数?你能检查一下我的代码中是否还有其他错误吗 import turtle as t def how_many(): while True: print " How many of then do you want to draw?" print " (Range is from 1 to 5)" shape_

我是python新手,正在做这个家庭作业。我需要创建一个带有菜单的小程序。直到现在我还很好。我有点迷路了。如何将值传递给函数?你能检查一下我的代码中是否还有其他错误吗

import turtle as t


def how_many():
    while True:  
        print "  How many of then do you want to draw?"
        print "  (Range is from 1 to 5)"
        shape_no = raw_input('  Enter your choice: ')
        try: 
            shape_no = int(shape_no)
            if (1 <= shape_no <= 5):
                print "Your number is ok"
                break
            else:
                print
                print "from 1 to 5 only"

        except:
            print "Only numbers allowed - Please try again"
    return True


#=========#=========#=========#=========#=========#=========#=========#


def draw_square():
    t.penup() 
    t.setpos(-200,0) 
    t.pendown()

    Number_of = 5
    for a in range(Number_of): 
        for a in range(4): 
            t.forward(60) 
            t.left(90)
        t.penup() 
        t.forward(80)
        t.pendown()

#=========#=========#=========#=========#=========#=========#=========#
def main():
    while True:
        print
        print "  Draw a Shape"
        print "  ============"
        print
        print "  1 - Draw a square"
        print
        print "  X - Exit"
        print

        choice = raw_input('  Enter your choice: ')

        if (choice == 'x') or (choice == 'X'):
            break
        elif choice == '1':
            how_many()
            draw_square()
        else:
            print 'Try again'



#=========#=========#=========#=========#=========#=========#=========#
if __name__ == "__main__":
    main()

#=========#=========#=========#=========#=========#=========#=========#
将海龟作为t导入
定义有多少个()
尽管如此:
打印“那么你想画多少?”
“打印”(范围从1到5)
shape_no=原始输入('输入您的选择:')
尝试:
形状编号=整数(形状编号)

if(1有关如何定义和调用函数,请参阅


有关代码检查,请尝试查看。有关如何定义和调用函数,请参阅


对于代码审阅,请尝试

我希望能够绘制用户请求绘制的正方形数量。我希望能够绘制用户请求绘制的正方形数量。我得到名称错误:当我绘制正方形时未定义全局名称“shape_no”(shape_no)``def draw_square(n):t.penup()t.setpos(-200,0)t.pendown()函数可以返回一个值:I get name错误:当我
draw_square(shape_no)``def draw_square(n)时,没有定义全局名称'shape_no':t.penup()t.setpos(-200,0)t.pendown()Number of=n对于范围内(Number of):对于范围内(4):t.forward(60)t.left(90)t.penup()t.forward(80)t.pendown()
函数可以返回一个值: