'How to modify a global variable in python and how to add a list to an array that is returned by a function
I'm currently having issues with a program I'm making. It has four functions: Decodes the string (obligatory to use it as string) into an array:
list = 'Ana;Mora;Tencio°3-3456-0987°F ^Juan;Arias;Quirós°1-0654-1345°M ^Enrique;Miranda;Ortega°5-6547-8745°M ^' \'María;Montero;Fonseca°1-6654-7458°F ^Angela;Montero;Fonseca°2-6654-7458°F'
def decodeList():
person = list.split('^')
sepNCG = [sep.split('°') for sep in person]
names= [sepX[0] for sepX in sepNCG]
name = [i.split(';',1)[0] for i in names]
firstLn= [i.split(';',2)[1] for i in names]
secondLn = [i.split(';',3)[2] for i in names]
code= [code[1] for code in sepNCG]
gender = [gen[2] for gen in sepNCG]
n = 0
array= []
while n != len(person):
person = [[name[n],firstLn[n],secondLn[n]],code[n],gender[n]]
n += 1
array.append(person)
return array
Now the second one should add a new person to the string (the parameters come from an input function):
def add(name,firstLn,secondLn,code,gender):
global list
if gender == True:
gender= 'M'
elif gender == False:
gender = 'F'
person = '^'+name+';'+firstLn+';'+secondLn+'°'+code+'°'+gender+'^'
list = list + person
return list
Now, this returns the string with the new data, but if I were to run the decode function again (using a menu so the program isn't stopped) the new data isn't there.
Now for the third one:
def removePerson(code):
global lista
for i in lista:
if code in i:
lista.remove(i)
return lista
elif cedula not in lista:
return 'Person not in list'
With this one happens the same, so my question is how do I do change it so that add and removePerson change the string so that when I run the decodeList function it does it with the changes previously made?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
