Invictus
Invictus

Reputation: 2780

Getting nameerror in python

I have the following function, I want to concat the 2 strings, What wrong am I doing here?

commands = ["abcd","123"]

def configure_dev(self, steps):
    func_name = self.id + ':configure dev'

    global conf_cmd
    for key in commands:
        conf_cmd += key + '\n'
    print(conf_cmd)

Getting the following error:

conf_cmd += key + '\n'

After running it, I get this error: NameError: name 'conf_cmd' is not defined

Upvotes: 1

Views: 57

Answers (2)

Fallenreaper
Fallenreaper

Reputation: 10682

I added your code with your critical issue resolved.

commands = ["abcd","123"]
def configure_dev(self, steps):
  func_name = self.id + ':configure dev'
  global conf_cmd = ''  //  <-- ''
  for key in commands:
    conf_cmd+=key+'\n'
  print(conf_cmd)

Upvotes: 1

Or Duan
Or Duan

Reputation: 13810

All you need to do is to add: conf_cmd = ''

just after commands = ["abcd","123"]

Why? global conf_cmd Does not create new string, it just means you can access the global variable.

Upvotes: 1

Related Questions