Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:02:25+00:00 2026-06-12T08:02:25+00:00

I’m trying to use the Turtle module in Python to draw the American flag

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:02:26+00:00Added an answer on June 12, 2026 at 8:02 am

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.