Urgent Help Needed For Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shakshakshuk
    New Member
    • Oct 2008
    • 4

    Urgent Help Needed For Python

    Hi everyone,

    I have recently been introduced to python in my university and now I have an assignment to submit.
    My problem is that I don't have much python programming experience.

    I was asked to write a program to:
    1. Enter coefficients
    2. check solvability
    3. Do gauss seidel
    4. Do gauss jacobi

    I have started writing my "simple" program when I encountered an error which I can't debug. Here are my latest codings:

    Code:
    def menu():
        print"(1) Enter coefficients"
        print"(2) Check solvability"
        print"(3) Gauss-Seidel"
        print"(4) Gauss-Jacobi"
    
        choice=input("Your choice is:")
        if 1<=choice<=4:
            if choice==1:
                coeff()
            if choice==2:
                check()
        else:
            print
            print"ERROR! Either wrong number chosen or letters have been entered."
            print"Please choose a number from 1 to 4 only."
            print
            
    def coeff():
        try:
            global a1
            global b1
            global c1
            global a2
            global b2
            global c2
            global a3
            global b3
            global c3
            global d1
            global d2
            global d3
                    
            a1=input("a1=")
            b1=input("b1=")
            c1=input("c1=")
            a2=input("a2=")
            b2=input("b2=")
            c2=input("c2=")
            a3=input("a3=")
            b3=input("b3=")
            c3=input("c3=")
            d1=input("d1=")
            d2=input("d2=")
            d3=input("d3=")
            print
        except:
            print
            print"Error occured! Please check and re-enter your values."
            coeff()
            print
            
    def check():
        if a1<0:
            a1=-a1
        if b1<0:
            b1=-b1
        if c1<0:
            c1=-c1
        if a2<0:
            a2=-a2
        if b2<0:
            b2=-b2
        if c2<0:
            c2=-c2
        if a3<0:
            a3=-a3
        if b3<0:
            b3=-b3
        if c3<0:
            c3=-c3
    
        if a1>b1+c1:
            if b2>a2+c2:
                if c3>a3+b3:
                    print"This system of linear equations can be solved using Gauss-Jacobi and Gauss-Seidel."
                else:
                    print"This system of linear equations cannot be solved using Gauss-Jacobi and Gauss-Seidel."
            
    
    
    while True:
        menu()
    I haven't done the part on gauss seidel and gauss jacobi. But concerning the second choice in my menu, it returns the :
    "UnboundLocalEr ror: local variable 'a1' referenced before assignment"

    I really don't know what to do and I have only 2 weeks to submit my assignment. Your assistance will be of a great help.

    Thank you in advance for the help you have provided to me and please excuse me if my english was not good.
    Last edited by numberwhun; Oct 13 '08, 10:28 PM. Reason: Please use code tags!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    First, please use CODE tags when posting your code. This is especially important with Python because the forum parser strips out all that syntactic whitespace.

    The reason that it's complaining about a1 is that a1 isn't declared anywhere. It'd complain about any of your other global variables for the same reason. Just because you say you want to use a global variable in a function, it doesn't follow that the global variable exists. If you want to do it with a global, then somewhere in global scope, you need to declare that. I would assume you should initialize it to 0.

    Comment

    • labmonkey111
      New Member
      • Sep 2008
      • 44

      #3
      You need the global declarations in all of you functions. Python assumes all variables are local unless you explicitly say otherwise.

      Comment

      • shakshakshuk
        New Member
        • Oct 2008
        • 4

        #4
        Originally posted by labmonkey111
        You need the global declarations in all of you functions. Python assumes all variables are local unless you explicitly say otherwise.

        Thank you again for your help. Declaring the global variables in each function, including my main program was a great idea. It finally works.
        All I need to do now is to calculate gauss seidel and gauss jacobi.

        I have quite a good idea how to do the gauss seidel, but I am having problems using gauss jacobi as whenever the value of x1 has been calculated, it replaces it in my next equation, like in gauss seidel.

        Code:
        x1=(d1-b1y0-c1z0)/a1
        y1=(d2-a2x1-c2z0)/b2
        z1=(d3-a3x1-b3y1)/c3
        This is one line for my code for gauss seidel. It works perfectly. The problem is with gauss jacobi. The x1 in the second and third equation should be x0 and y1 in the third equation should be y0. But when I change these to x0 and y0, my loop fails to work.
        Last edited by numberwhun; Oct 13 '08, 11:14 PM. Reason: Please use code tags!

        Comment

        • labmonkey111
          New Member
          • Sep 2008
          • 44

          #5
          What are the terms you have like this: b1y0?

          Is 'b1y0' a variable name? Or do you mean it to be multiplication? Like b1*y0?

          Comment

          • shakshakshuk
            New Member
            • Oct 2008
            • 4

            #6
            Originally posted by labmonkey111
            What are the terms you have like this: b1y0?

            Is 'b1y0' a variable name? Or do you mean it to be multiplication? Like b1*y0?

            I am really very sorry for this error. What I meant was b1*y0.
            Sorry about this mistake again. The fact was that I had to work till late on my second assignment and I was feeling a bit sleepy when I posted my request

            Comment

            Working...