Variable error??? Need Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolindienc
    New Member
    • Aug 2006
    • 48

    Variable error??? Need Help

    I am trying to figure out what is wrong with this code. It says that global variable is not defined. Below is the error and code of the program. I have also highlighted the error place. Please help.

    Andy

    ERROR
    Traceback (most recent call last):
    File "F:/Python/Python/Tests/Hardik Desai Test 3A.py", line 43, in -toplevel-
    calculate()
    File "F:/Python/Python/Tests/Hardik Desai Test 3A.py", line 19, in calculate
    area = length * width
    NameError: global name 'length' is not defined


    Code:
    def tableOrder():
        orderNum = int (raw_input ("Enter the order number: "))
        if (orderNum == 0):
            print "Thank you for visiting 'Woodchop Shop'\n"
            raw_input ("Press Enter to exit!!!")
        else:
            length = int (raw_input ("Enter the length of the table in inches: "))
            width = int (raw_input ("Enter the width of the table in inches: "))
            woodType = (raw_input ("Enter type of wood you would like for the table: "))
            drawerNum = int (raw_input("Enter the numbers of drawer you would like: "))
    
    def calculate():
        global drawerNum, length, width
        [B]area = length * width[/B]
        baseCharge = 200.00
        if area <= 750:
            charge1 = baseCharge
        elif area > 750:
            charge1 = baseCharge + 50.00
            if woodType == "mahogany":
                charge2 = 150.00
            elif woodType == "oak":
                charge2 = 125.00
            elif woodType == "pine":
                charge2 = 0.00
            charge3 = drawerNum * 30.00
            totalCharge = charge1 + charge2 + charge3
    
    def getOutput():
        print "Order Number : ",orderNum
        print "Size of Table : ", area
        print "Type of Wood : ",woodType
        print "Total charge for the table: ",totalCharge
    
    #Main------------------------------------------
    global area, length, width
    tableOrder()
    [B]calculate()[/B]
    getOutput
    while ans == "y" or ans == "Y":
        tableOrder()
        calculate()
        getOutput()
        raw_input("Would you like to order another table?? (y/n)")
    raw_input ("Press enter to exit....")
    Last edited by bartonc; Dec 5 '06, 08:01 PM. Reason: CODE tags must be in CAPS
  • fuffens
    New Member
    • Oct 2006
    • 38

    #2
    In order for length to be global in the calculate function it must global the tableOrder function as well. But you really should not be using global variables unless you really have to (or writing something so simple that it doesn't matter).

    BR
    /Fredrik

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by fuffens
      In order for length to be global in the calculate function it must global the tableOrder function as well. But you really should not be using global variables unless you really have to (or writing something so simple that it doesn't matter).

      BR
      /Fredrik
      I agree with Fredrik 100%. But to make it work change

      Code:
      #Main------------------------------------------
      global area, length, width
      to
      Code:
      #Main------------------------------------------
      area, length, width = 0, 0, 0
      or
      Code:
      #Main------------------------------------------
      area = length = width = 0
      and initialize any other vars that are global in functions the same way.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by fuffens
        .....But you really should not be using global variables unless you really have to (or writing something so simple that it doesn't matter).

        BR
        /Fredrik
        Right on Fredrik.

        Andy - check out this link: https://en.wikipedia.org/wiki/Object...ed_programming

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Andy, please use CAPs in the CODE tags, thanks,
          Barton

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Also, this is correct:

            Code:
            def calculate():
                global drawerNum, length, width
                area = length * width
                baseCharge = 200.00
                if area <= 750:
                    charge1 = baseCharge
                elif area > 750:
                    charge1 = baseCharge + 50.00
                    if woodType == "mahogany":
                        charge2 = 150.00
                    elif woodType == "oak":
                        charge2 = 125.00
                    elif woodType == "pine":
                        charge2 = 0.00
                    charge3 = drawerNum * 30.00
                    totalCharge = charge1 + charge2 + charge3
            This is incorrect:
            Code:
            def getOutput():
                print "Order Number : ",orderNum
                print "Size of Table : ", area
                print "Type of Wood : ",woodType
                print "Total charge for the table: ",totalCharge

            Comment

            • coolindienc
              New Member
              • Aug 2006
              • 48

              #7
              Thanks guys,
              However do you think if there is anything wrong with my loop?

              Andy

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by coolindienc
                Thanks guys,
                However do you think if there is anything wrong with my loop?

                Andy
                Code:
                ans = ""    # all variables must be assigned before they are referenced
                while ans[0].lower() == "y":    # in case there is more that 1 chr typed
                    tableOrder()
                    calculate()
                    getOutput()    # Don't forget to put your input somewhere
                    ans = raw_input("Would you like to order another table?? (y/n)")
                raw_input ("Press enter to exit....")

                Comment

                Working...