473,378 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:

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.
Oct 12 '08 #1
5 1860
Laharl
849 Expert 512MB
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.
Oct 12 '08 #2
You need the global declarations in all of you functions. Python assumes all variables are local unless you explicitly say otherwise.
Oct 12 '08 #3
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.
Oct 13 '08 #4
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?
Oct 13 '08 #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?

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
Oct 14 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: prabu | last post by:
Hello All, I am using Fsh,it has Python Dependecy.It uses several moduels from Python.The problem is"Select" module in missing in the list of modules available in python.So I can't import it.But...
0
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
0
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
4
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application running on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
7
by: meenasamy | last post by:
Hi all, i need to create a function that takes three parameters( Original currency, needed currency, amount) i need to convert from the original currency to the needed currency the amount and...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
5
by: Aykut Canturk | last post by:
Dear friends, I recently decided to move vb.net 2005 from vb6. my projects has average of 200 forms and 20 modules. I mostly write enterprise solution about production automation like...
1
by: alibaaba | last post by:
HI All, i am a 4th year business student and i took web design online course for fun however i did not see that last 2 chapters were python programming.This has no relevance to my major nor does...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.