Python Forum

Full Version: NameError: name ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
on line #1 variable is not defined. What you expect?
(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))   
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'}
(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