I need to read the value of a variable that is local declared in a function, by scoping another function, in Python script

Asked

Viewed 18 times

-1

I have a code that is very similar to the following

def um():
   x = 'abc'
   print(x)

def dois():
   y = 'bcd'+ 'x'
   print(y)

It turns out that, like x is local in function um, the function dois can’t read the x, returning an error that informs 'x has not been declared'.

1 answer

0

In function um is asking to print the amount x, the correct one would return this value. Already in the function dois, the variable y receives the concatenation of two strings: 'bcd' and x.

I believe the intention is to have access to the variable x of function um.

And once again is printing the variable in question instead of returning it.

def um():
x = 'abc'
return x


def dois():
    y = 'bcd' + um()
    return y


print(dois())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.