Python Forum
NameError: name ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: NameError: name ? (/thread-24634.html)



NameError: name ? - JohnnyCoffee - Feb-24-2020

I need to get the value of the variable below that stores a dictionary, why do I get the answer (NameError: name 'variable' is not defined) ?

Exemple :
variable['txt'] = 'welcome'
print(variable)



RE: NameError: name ? - buran - Feb-24-2020

on line #1 variable is not defined. What you expect?


RE: NameError: name ? - JohnnyCoffee - Feb-24-2020

(Feb-24-2020, 11:36 AM)buran Wrote: on line #1 variable is not defined. What you expect?
Do I need to receive the text in json format but still have the same error?

import json                  
my_var['João'] = '8887-7778'
print(json.dumps(my_var))   



RE: NameError: name ? - jefsummers - Feb-24-2020

Just tell Python it's a dictionary. The syntax could be for list and so it is confused
variable = dict()
variable['txt'] = 'welcome'
print(variable)
Output:
{'txt': 'welcome'}



RE: NameError: name ? - JohnnyCoffee - Feb-24-2020

(Feb-24-2020, 12:23 PM)jefsummers Wrote: Just tell Python it's a dictionary. The syntax could be for list and so it is confused
variable = dict()
variable['txt'] = 'welcome'
print(variable)
Output:
{'txt': 'welcome'}

Tanks, solved