Invictus Invictus - 4 months ago 18
Python Question

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'

NameError: name 'conf_cmd' is not defined

Answer

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)
Comments