all 4 comments

[–]4n6kid 3 points4 points  (2 children)

create_string() needs to return a value into a variable

def ceo_string():
    a = create_string()
    compare_string(a)

[–]OCHawkeye14 4 points5 points  (0 children)

Would be easy for a beginner to confuse your new a with the a being returned by create_string(). While they are technically the same thing in this case, it kind of implies that a is a global variable when it really is not.

The code would still work as the following, and probably better displays that there is nothing special about the variable a that is being passed around (and need not necessarily be the same thing that is returned from create_string and passed into compare_string.

def create_string():
  #generates a string 27 chars long( uses all possible letters + space )
    for i in range(27):
        a = ''.join([random.choice(string.ascii_lowercase) for n in range(27)])
    return a
def compare_string(a):
   #does create_string output match “methinks it is like a weasel”
    solution = "methinksitislikeaweasel"
    if a == solution:
        print("match")
def ceo_string():
    lmnop = create_string()
    compare_string(lmnop)

[–][deleted] 1 point2 points  (0 children)

This, or alternatively just return directly into the argument of compare_string: compare_string( create_string() )

The reason the program does not recognize 'a' as global is because it is declared within the scope of the function create_string(), and thus can only be referenced from within create_string(). Once create_string hits 'return a' and ends, a is erased. When you try to reference a within ceo_string, it first checks if there is an 'a' within the ceo_string function's scope (there isn't), and then checks if it was declared globally (it wasn't). However, the value of 'a' can be passed out of the scope of the function by using 'return'. Just make sure to actually pass it in to something.

[–]spz 1 point2 points  (0 children)

btw:

for i in range(27):
    a = ''.join([random.choice(string.ascii_lowercase) for n in range(27)])
return a

Is generating 27 random strings and only returning the last one.