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)



Wao how is that possible





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 User Page / My Talk Page / My website /
——————–
Progress bar to 1000+ Posts - Image not updated lots (Progress last updated: I forget to update this :P)

——————–
My browser / operating system: Windows NT 10.0, Firefox 63.0b10, Flash 31.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.


Wao how is that possible





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?
^^^

bf97b44a7fbd33db070f6ade2b7dc549

you get a cookie

Powered by DjangoBB

Standard | Mobile