-2

So I want to pass two lists into my function. The aim of my program is to solve a system of three linear equations using the Gauss Jordan Method.

import sys
from  tkinter import *

def elim(x,list_a,list_b):
    r=-list_b[x]/list_a[x]
    for i in range(x-1,len(list_b)+1):
        list_b[i]=list_b[i]+r*list_a[i]
    return list_b

def mhello():
    ma1=a1.get()
    ma2=a2.get()
    ma3=a3.get()
    ma4=a4.get()
    mb1=b1.get()
    mb2=b2.get()
    mb3=b3.get()
    mb4=b4.get()
    mc1=c1.get()
    mc2=c2.get()
    mc3=c3.get()
    mc4=c4.get()
lista=(int(ma1),int(ma2),int(ma3),int(ma4))
listb=(int(mb1),int(mb2),int(mb3),int(mb4))
listc=(int(mc1),int(mc2),int(mc3),int(mc4))

elim(1,lista,listb)
print(listb)

when I do enter the values, I get the following error:

File "C:/Users/madhuri/Desktop/gauss.py", line 29, in mhello
    elim(1,lista,listb)
  File "C:/Users/madhuri/Desktop/gauss.py", line 6, in elim
    list_b[i]=list_b[i]+r*list_a[i]
TypeError: 'tuple' object does not support item assignment

What am I doing incorrectly? How can I pass a list to a fucntion as an argument? How can I return a list?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
user8244
  • 127
  • 1
  • 5
  • list and tuple are different things. You are sending tuple not list. Further reading: [What's the difference between lists and tuples?](http://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples) – Lafexlos Dec 23 '16 at 14:49
  • in addition, you're going to get an IndexError in your elim function. – acushner Dec 23 '16 at 14:53

1 Answers1

3

You aren't passing lists, though. Enclosing comma separated values in parentheses (or without them) will yield a tuple instance, not a list.

The fact that you send tuples to your function then fails when you try to assign to an element of that tuple with list_b[i] = ... because tuples are immutable collections.

If you need lists you should enclose in brackets:

lista= [int(ma1),int(ma2),int(ma3),int(ma4)]
listb= [int(mb1),int(mb2),int(mb3),int(mb4)]
listc= [int(mc1),int(mc2),int(mc3),int(mc4)]
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253