Connecting Tech Pros Worldwide Forums | Help | Site Map

Urgent Help Needed For Python

Newbie
 
Join Date: Oct 2008
Posts: 4
#1: Oct 12 '08
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:

Expand|Select|Wrap|Line Numbers
  1. def menu():
  2.     print"(1) Enter coefficients"
  3.     print"(2) Check solvability"
  4.     print"(3) Gauss-Seidel"
  5.     print"(4) Gauss-Jacobi"
  6.  
  7.     choice=input("Your choice is:")
  8.     if 1<=choice<=4:
  9.         if choice==1:
  10.             coeff()
  11.         if choice==2:
  12.             check()
  13.     else:
  14.         print
  15.         print"ERROR! Either wrong number chosen or letters have been entered."
  16.         print"Please choose a number from 1 to 4 only."
  17.         print
  18.  
  19. def coeff():
  20.     try:
  21.         global a1
  22.         global b1
  23.         global c1
  24.         global a2
  25.         global b2
  26.         global c2
  27.         global a3
  28.         global b3
  29.         global c3
  30.         global d1
  31.         global d2
  32.         global d3
  33.  
  34.         a1=input("a1=")
  35.         b1=input("b1=")
  36.         c1=input("c1=")
  37.         a2=input("a2=")
  38.         b2=input("b2=")
  39.         c2=input("c2=")
  40.         a3=input("a3=")
  41.         b3=input("b3=")
  42.         c3=input("c3=")
  43.         d1=input("d1=")
  44.         d2=input("d2=")
  45.         d3=input("d3=")
  46.         print
  47.     except:
  48.         print
  49.         print"Error occured! Please check and re-enter your values."
  50.         coeff()
  51.         print
  52.  
  53. def check():
  54.     if a1<0:
  55.         a1=-a1
  56.     if b1<0:
  57.         b1=-b1
  58.     if c1<0:
  59.         c1=-c1
  60.     if a2<0:
  61.         a2=-a2
  62.     if b2<0:
  63.         b2=-b2
  64.     if c2<0:
  65.         c2=-c2
  66.     if a3<0:
  67.         a3=-a3
  68.     if b3<0:
  69.         b3=-b3
  70.     if c3<0:
  71.         c3=-c3
  72.  
  73.     if a1>b1+c1:
  74.         if b2>a2+c2:
  75.             if c3>a3+b3:
  76.                 print"This system of linear equations can be solved using Gauss-Jacobi and Gauss-Seidel."
  77.             else:
  78.                 print"This system of linear equations cannot be solved using Gauss-Jacobi and Gauss-Seidel."
  79.  
  80.  
  81.  
  82. while True:
  83.     menu()
  84.  
  85.  
I haven't done the part on gauss seidel and gauss jacobi. But concerning the second choice in my menu, it returns the :
"UnboundLocalError: 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.

Expert
 
Join Date: Sep 2007
Posts: 856
#2: Oct 12 '08

re: Urgent Help Needed For Python


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.
Member
 
Join Date: Sep 2008
Posts: 40
#3: Oct 12 '08

re: Urgent Help Needed For Python


You need the global declarations in all of you functions. Python assumes all variables are local unless you explicitly say otherwise.
Newbie
 
Join Date: Oct 2008
Posts: 4
#4: Oct 13 '08

re: Urgent Help Needed For Python


Quote:

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.

Expand|Select|Wrap|Line Numbers
  1. x1=(d1-b1y0-c1z0)/a1
  2. y1=(d2-a2x1-c2z0)/b2
  3. z1=(d3-a3x1-b3y1)/c3
  4.  
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.
Member
 
Join Date: Sep 2008
Posts: 40
#5: Oct 13 '08

re: Urgent Help Needed For Python


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?
Newbie
 
Join Date: Oct 2008
Posts: 4
#6: Oct 14 '08

re: Urgent Help Needed For Python


Quote:

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
Reply