0

Variables not working in functions, why? What's wrong in My Code? I checked many times but everything is current. Please Help 🥺

Mode = "Copy"


#mode
def runRclone():
  if Mode == "Copy":
    print ("cpy")
    modeFinal = "copy"
  elif Mode == "Move":
    modeFinal = "move"
  elif Mode == "Check Size":
    modeFinal = "size"
  else:
    modeFinal = "size"
    print("Some Thing is Wrong, Contact me!")

runRclone()

print(modeFinal)

Output

cpy
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-7ade1dfbd321> in <module>()
     42 runRclone()
     43 
---> 44 print(modeFinal)

NameError: name 'modeFinal' is not defined

3 Answers 3

1

You call your function. modeFinal is defined in your function but not global. You can make your modeFInal global at the start of your function like global modeFinal.

Mode = "Copy"


#mode
def runRclone():
  global modeFinal
  if Mode == "Copy":
    print ("cpy")
    modeFinal = "copy"
  elif Mode == "Move":
    modeFinal = "move"
  elif Mode == "Check Size":
    modeFinal = "size"
  else:
    modeFinal = "size"
    print("Some Thing is Wrong, Contact me!")

runRclone()

print(modeFinal)

Another option would be to return modeFinal in your function then store the function output in variable.

Mode = "Copy"


#mode
def runRclone():
  if Mode == "Copy":
    print ("cpy")
    modeFinal = "copy"
  elif Mode == "Move":
    modeFinal = "move"
  elif Mode == "Check Size":
    modeFinal = "size"
  else:
    modeFinal = "size"
    print("Some Thing is Wrong, Contact me!")
   return modeFinal
mdFinal = runRclone()

print(mdFinal)
Sign up to request clarification or add additional context in comments.

Comments

1

modeFinal is not being returned and is scoped to be within the function, so when you call print(modeFinal) it cannot find it. You could return it at the end of your if/elif statement

Mode = "Copy"


#mode
def runRclone():
  if Mode == "Copy":
    print ("cpy")
    modeFinal = "copy"
  elif Mode == "Move":
    modeFinal = "move"
  elif Mode == "Check Size":
    modeFinal = "size"
  else:
    modeFinal = "size"
    print("Some Thing is Wrong, Contact me!")

  return(modeFinal)

modeFinal = runRclone()

print(modeFinal)

Comments

1

modeFinal is only visible within the scope of your function runRclone(). You could have your function return modeFinal as such :

Mode = "Copy"

#mode
def runRclone():
  if Mode == "Copy":
    print ("cpy")
    modeFinal = "copy"
  elif Mode == "Move":
    modeFinal = "move"
  elif Mode == "Check Size":
    modeFinal = "size"
  else:
    modeFinal = "size"
    print("Some Thing is Wrong, Contact me!")

  return modeFinal

modeFinal = runRclone()

print(modeFinal)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.