Discuss Scratch

1a3c5e7g9i
Scratcher
100+ posts

Declaring variables with python

#I'm trying to declare variables within a Python program like this:
def declare(var):
    exec("%s = ''"%(var))
#When I declare a variable with it, it doesn't recognize the parameter as a variable.
#Is there something I missed?
This will stay open for more potential workarounds.

Last edited by 1a3c5e7g9i (Dec. 30, 2017 21:08:30)


Uh Squidward? Yes Spongebob? You might want to look at this… HOLY SHRIMP! If we don't act now, well, think about how many clams I'll have to cough up JUST to see an extra portion of nick.com… or worse, to see ALL of it in general! *** Reader! This is not a fictional sketch! It actually is, but that's not the point. Learn more at the link that Spongebob left at the first instance of “this”.
bobbybee
Scratcher
1000+ posts

Declaring variables with python

Why do you want to do this?

“Ooo, can I call you Señorita Bee?” ~Chibi-Matoran
WooHooBoy
Scratcher
1000+ posts

Declaring variables with python

The variable is declared, but in the scope of the declare function:
def declare(var):
exec("%s = 'lol'"%(var))
exec("print(%s)"%(var))
works just fine. Wouldn't you be better off using a dict instead?

considered harmful
Mrcomputer1
Scratcher
500+ posts

Declaring variables with python

This might work:
def declare(var):
    exec("%s = ''"%(var), globals(), globals())

My Profile / OS Projects / My Test Account / Bored Activities / My website /
——————–
Progress bar to 500+ Posts - Image not updated lots (Progress last updated: I forget to update this :P)

——————–
My browser / operating system: Windows 10 (NT 10.0), Firefox 49.0, Flash 23.0 (release 0)
——————–
My Scratch Extensions——–If you like this post then give me an internet!——–Sharp Scratch Mod
1a3c5e7g9i
Scratcher
100+ posts

Declaring variables with python

Mrcomputer1 wrote:

This might work:
def declare(var):
    exec("%s = ''"%(var), globals(), globals())
It worked. Thank you.

Uh Squidward? Yes Spongebob? You might want to look at this… HOLY SHRIMP! If we don't act now, well, think about how many clams I'll have to cough up JUST to see an extra portion of nick.com… or worse, to see ALL of it in general! *** Reader! This is not a fictional sketch! It actually is, but that's not the point. Learn more at the link that Spongebob left at the first instance of “this”.
TheUltimatum
Scratcher
1000+ posts

Declaring variables with python

bobbybee wrote:

Why do you want to do this?
^^^

Powered by DjangoBB

Standard | Mobile