Chris
Chris

Reputation: 9

Combining 2 functions

I am trying to combine two functions because A) I think I can and B) I think I should. When I use the functions separately the script works fine. When I combine them the "new" variable is an empty string vice what it should be. Any help would be appreciated. If you need the full script that could be arranged.

Function A:

def strip_domain_name(x):
    global ns
    l = x.find('@')
    ns = x[0:l]

Function B:

def encode_user_name(x,y):
    global new
    for a in x:
        if a in y:
            new = new + y.get(a)

Function A+B:

def combined_above_script(x,y,z):
    global ns
    global new  
    l = x.find('@')
    ns = x[0:l] 
    for a in y:
        if a in z:                  
            new = new + z.get(a)

Here is a simplified version of what I'm trying to do, with some modifications based on blender's suggestion. In the end if I print aa it should result in '0000000'. Which is not the case.

aa = ''
bb = ''
encode = {'a':'0'}

def strip_and_encode(x,y,z):
aa = '' 
bb = x[0:x.find('@')]   

for a in y:
    if a in z:                  
        aa += z.get(a)

s='aaaaaaa@aaa'
strip_and_encode(s,bb,encode)
print(aa)

Upvotes: 0

Views: 504

Answers (1)

Blender
Blender

Reputation: 298156

I'd go easy with the globals:

def script(x, y, z):
  new = ''
  ns = x[:x.find('@')]

  for a in y:
    if a in z:                  
      new += z.get(a)

Upvotes: 2

Related Questions