why do I get a keyerror | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
27th Jun 2020, 6:39 PM
Oma Falk
Oma Falk - avatar
8 Answers
+ 3
Oma Falk , I think better use globals() function. Look at the code 🐱 https://code.sololearn.com/cEPJQGHXMi9Q/?ref=app
27th Jun 2020, 7:08 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 4
Jannik Müller thanks yes it helped. I stood on the pipeline. Noch völlig berauscht vom Werderdusel💪
27th Jun 2020, 7:25 PM
Oma Falk
Oma Falk - avatar
+ 3
Oma Falk Ich schreibe es jetzt einfach mal auf Deutsch, weil es etwas schneller geht: Lokale Variablen sind Variablen, die wieder gelöscht werden, wenn die Funktion, in der sie definiert wurden, durchgelaufen ist. Globale Variablen sind Variablen, die so lange wie der Prozess des Programms selbst erhalten bleiben. (Außer man löscht sie) Variablen, die außerhalb einer Funktion deklariert werden, sind standardmäßig global, und Variablen, die in einer Funktion deklariert werden, sind standardmäßig lokal. Wenn man eine globale Variable in einer Funktion deklarieren / weiterverwenden will, dann kann man das mit "global <Name>" machen. Ich hoffe, das hat geholfen. (Sorry to all the English speaking people, I had to write this one in German)
27th Jun 2020, 7:14 PM
Jannik Müller
Jannik Müller - avatar
+ 3
translation Oma Falk I just write it in German because it is a bit faster: Local variables are variables that are deleted again when the function in which they were defined has run through. Global variables are variables that remain as long as the process of the program itself. (Unless you delete them) Variables that are declared outside of a function are global by default, and variables that are declared in a function are local by default. If you want to declare / continue to use a global variable in a function, you can with "global <name>". I hope that helped. (Sorry to all the English speaking people, I had to write this one in German)  
27th Jun 2020, 8:11 PM
Oma Falk
Oma Falk - avatar
27th Jun 2020, 7:09 PM
Oma Falk
Oma Falk - avatar
+ 2
haaaaaaarg locals are the locals of dict comprehension which creates a closure😱😱😱
27th Jun 2020, 7:10 PM
Oma Falk
Oma Falk - avatar
+ 1
I think you have to replace locals() with globals(), because the variables were not declared in a function.
27th Jun 2020, 7:08 PM
Jannik Müller
Jannik Müller - avatar
+ 1
I want to understand one thing In below program, Can't we update globals() outside the comprehension? https://code.sololearn.com/c6Em4Yl1l59i/?ref=app Could you people help? basically I want to understand the trick inside below function, def func(args): [*map(globals().update, [{a: val(a)} for a in args.split()])]
27th Jun 2020, 8:20 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar