了解Python中的函数 [英] Understanding Functions in Python

查看:204
本文介绍了了解Python中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python中使用Turtle模块来使用单独的函数绘制美国国旗,首先我有draw_flag计算所有尺寸,然后draw_rectangle实际绘制标志形状,然后生成draw_stars等,绘制星星和线条。



但是,我对于函数的工作原理有点麻烦..到目前为止我的代码是:

 导入turtle 
导入时间
导入随机

def draw_rectangle(长度,高度):
turtle.up( )
x =长度
y =高度

turtle.begin_fill()
turtle.setpos(x,y)
turtle.down()
turtle.forward(418)
turtle.right(90)
turtle.forward(220)
turtle.right(90)
turtle.forward(418)
turtle.right(90)
turtle.forward(220)
turtle.end_fill()

turtle.sleep(2)
turtle.bye

def draw_flag(A):
height = int(A)
## length = height * 1.9
## union_height = height *(7/13)
## union_length = length *(2/5)
## E = F = union_height / 10
## G = H = union_length / 12
## stripe_width = height / 13
## diameter_star = stripe_width *(4/5)
长度=身高* 1。 9
返回

A = input(请输入标志的高度:)

draw_rectangle(长度,高度)

我希望能通过绘制一个矩形来返回,但它一直说长度没有定义,不确定将'length'在这里它会识别变量。

我使用Python 3,谢谢。

解决方案

这是一个考虑它的好方法。函数是一个黑匣子,它以特定的顺序接收一些名为 arguments 的值,并对它们做一些处理以产生其他值。当我说黑盒子时,我的意思是说,当你使用这个函数时,你不必关心它是如何做它的,它只是给它一些值,并且你得到一个值。

让我们考虑一个非常简单的函数,它只是减去它给出的两个数字:第一个减去第二个。换句话说,我们将实现一个实现规则的函数参数#1 - 参数#2。现在,当您为此函数编写代码时,需要某种方式告诉计算机何时使用参数1以及何时使用参数2。在其他一些编程语言中,您必须通过明确指定要使用的参数的编号(#1或#2)来完成此操作,但如果您可以提供这些值的名称,编写代码会更容易。所以Python和大多数其他语言一样,可以让你使用你选择的名字来引用一个函数的参数。例如,假设你想让参数#1进入名字 x ,参数#2进入名字 y 。你可以通过这样写明:

$ p $ def subtract(x,y):

接下来是构成函数的代码。对于减法示例,它将是

  def subtract(x,y):
return x - y

当Python编译器遇到这种情况时,它会将代码转换为其内部表示形式calculate value#1- value #2并将其发回给我的来电者。然后它打包这段代码并将其保存在名称 subtract 下(因为这是你告诉它你想命名的函数)。



希望这个代码块执行完后有意义,因为引用argument#1或argument#2不再有意义,因为没有功能就不能有参数!同样,一旦函数完成它的事情,你给参数的标签, x y ,不再有任何意义。标签仅在函数代码的持续时间内存在。这被称为范围限定:将标签限制在代码中它们意味着什么的部分。



因为标签 x 和 y 本地范围,正如人们所说的那样,它甚至不管它们是什么。例如,如果你在你的代码中定义了 subtract ,你可以任意决定将它们更改为 first 并且 second ,所有你必须改变的就是这个函数中的代码。您只需将定义改为

  def减去(第一,第二):
返回第一 - 第二

就是这样 - 你的代码在功能上完全一样。在程序中的任何其他地方都会出现 x y ,它们指的是这个函数的参数以外的东西,所以当你重新命名参数时,你不必改变它们。



你的情况发生了什么,你试图使用标签在你定义的函数之外(即你存储为 draw_rectangle )的函数之外的某个地方有一个长度的地方。 Python知道你不能引用你不在的函数的参数,所以它希望你已经定义了 length 来表示别的东西。但你没有。这就是为什么你会收到错误。 (好吧,那个错误,无论如何)

I'm trying to use the Turtle module in Python to draw the American flag using separate functions, first I have draw_flag which calculates all the dimensions, and then draw_rectangle which actually draws the flag shape, then ill make draw_stars, etc, to draw the stars and lines.

But, I am having a bit of trouble understanding on how functions work..here is my code so far:

import turtle
import time
import random

def draw_rectangle(length, height):
    turtle.up()
    x = length
    y = height

    turtle.begin_fill()
    turtle.setpos(x,y)
    turtle.down()
    turtle.forward(418)
    turtle.right(90)
    turtle.forward(220)
    turtle.right(90)
    turtle.forward(418)
    turtle.right(90)
    turtle.forward(220)
    turtle.end_fill()

    turtle.sleep(2)
    turtle.bye

def draw_flag(A):
    height = int(A)
##    length = height*1.9
##    union_height = height*(7/13)
##    union_length = length*(2/5)
##    E = F = union_height/10
##    G = H = union_length/12
##    stripe_width = height/13
##    diameter_star = stripe_width*(4/5)
    length = height*1.9
    return

A = input("Please enter the height of the flag: ")

draw_rectangle(length, height)

I was hoping it would return by drawing a rectangle, but it keeps saying length is not defined, not sure where to place 'length' where it will recognize the variable.

I'm using Python 3, thanks.

解决方案

Here's a good way to think about it. A function is a "black box" that takes in some number of values, called arguments, in a specific order and does something with them to produce some other value. When I say "black box," I mean that when you use the function, you don't have to care how it does what it does, you just give it some values and you get a value back.

Let's consider a very simple function that just subtracts the two numbers it's given: the first minus the second. In other words, we'll make a function that implements the rule "argument #1 - argument #2." Now, when you're writing the code for this function, you need some way to tell the computer when you want to use argument #1 and when you want to use argument #2. In some other programming languages, you have to do it by explicitly specifying the number of the argument you want to use (#1 or #2), but it's a lot easier to write code if you can give these values names. So Python, like most other languages, lets you refer to the arguments of a function using names of your choosing. For example, suppose you want argument #1 to go under the name x, and argument #2 to go under the name y. You could indicate that by writing this:

def subtract(x, y):

This would be followed by the code that constitutes the function. For the subtraction example, it would be

def subtract(x, y):
    return x - y

When the Python compiler encounters this, it translates the code into its internal representation of "calculate value #1 - value #2 and send that back to my caller." It then packs up that block of code and saves it under the name subtract (because that's what you told it you wanted to name the function).

Hopefully it makes sense that once this block of code finishes executing, it no longer makes any sense to refer to "argument #1" or "argument #2," because you can't have arguments without a function! So similarly, once the function has done its thing, the labels that you gave to the arguments, x and y, no longer have any meaning. The labels only exist for the duration of the function's code. This is called scoping: limiting labels to the part of the code where they mean something.

Because the labels x and y are locally scoped, as one might say, in a way it doesn't even matter what they are. For instance, if you had that definition of subtract in your code, you could arbitrarily decide to change them to first and second, and all you would have to change would be the code within that one function. You would just change the definition to

def subtract(first, second):
    return first - second

and that's it - your code is functionally exactly the same. Anywhere else in the program that x and y occur, they're referring to something other than the arguments of this function, so you don't have to change them when you rename the arguments.

What's happening in your case is that you tried to use the label length somewhere outside of the function it was defined for (namely, the function that you've stored as draw_rectangle). Python knows that you can't be referring to the argument of a function you're not in, so it expects you to have already defined length to mean something else. But you didn't. That's why you're getting an error. (Well, that one error, anyway)

这篇关于了解Python中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆