Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name ?
#1
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)
Reply
#2
on line #1 variable is not defined. What you expect?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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))   
Reply
#4
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'}
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,300 Oct-27-2019, 07:49 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020