0

Let's say I have a this script called script.py:

def num():
    ...
    return num1, num2

def quiz()
   ...
   ...
   num1, num2 = num()   
   name = input('name: ')
   class_num = input('class: ')

if __name__ = '__main__':
    quiz()

In another script I want to use this script and the script.py's defined variables:

import script

script.quiz()

def path(class_num):
    ... 
    # do stuff

This keeps on returning

'NameError: name 'class_num' is not defined'

How can I use all the variable defined in the script.py in my second script?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Matt
  • 43
  • 1
  • 2
  • Do both files reside on the same location?..I mean on the same folder – Iron Fist Feb 11 '16 at 18:53
  • Yep they do. They're both in the same folder – Matt Feb 11 '16 at 18:54
  • 1
    @Matt [You asked a very similar question earlier today](http://stackoverflow.com/questions/35341505/how-to-access-a-variable-defined-inside-a-function-from-outside-that-function). Have you read the answers there? – Håken Lid Feb 11 '16 at 19:04
  • Your script you gave us does not produce any error, it's correct. Accept for some small syntax errors. As i mentioned earlier, it is possible to use a parameter name that has the same name as a variable. So the line def path(class_num) is just fine. Please give us the code where the error occurse! – Andreas Müller Feb 12 '16 at 09:24

5 Answers5

4

you defined a function in the script that returns nothing, all the variables that are being defined by user input need to be returned

def quiz():
    stuff
    name = input('...')
    class_num = input('...')
    return [stuff, name, class_num]

the other scripts needs to be changed with

results = script.quiz()

then you parse results they will contain the quiz responses

user2255757
  • 756
  • 1
  • 6
  • 24
1

You are trying to make a local variable act as a global one.class_num has a scope within the function quiz, but not outside of it.

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

The error

'NameError: name 'class_num' is not defined'

Is telling you exactly that, this variable doesn't exist in the scope its called.

To fix the problem simply change

def quiz()
   ...
   ...
   num1, num2 = num()   
   name = input('name: ')
   class_num = input('class: ')
   return class_num

and in your other script catch the return variable

class_num = script.quiz()

def path(class_num):
Michal Frystacky
  • 1,418
  • 21
  • 38
  • {class_num} is a variable used within the function {quiz}. Its a str from the users input. You could declare it global or you could return it. – Michal Frystacky Feb 11 '16 at 19:02
  • I've read on many posts that it is not good practice to declare a variable global. I've tried returning class_num in the function quiz and then calling it but it is still returning the same error? – Matt Feb 11 '16 at 19:06
  • @Matt I added additional information at the end of my question that should help. Although you would want to deal with the input in your main module and pass the arguments to your other modules like script.py – Michal Frystacky Feb 11 '16 at 19:09
1

You can use something like this In your script.py return the class_num variable

def num():
    ...
    return num1, num2

def quiz()
   ...
   ...
   num1, num2 = num()   
   name = input('name: ')
   class_num = input('class: ')
   return class_num


if __name__ = '__main__':
    quiz()

and use it like this

import script

class_num = script.quiz()

def path(class_var = class_num):
    ... 
    # do stuff

in your code you are trying to use a local variable by importing the script.py which is not correct.

hope it may help !

sumit
  • 3,210
  • 1
  • 19
  • 37
  • What if I also want to use name, num1, num2 etc.? Do I have to repeat the same thing? – Matt Feb 11 '16 at 19:03
  • you can just return all of them through a list through return statement like this return [num1,num2,num3] – sumit Feb 11 '16 at 19:06
0

Python doesn't know what the variable class_num is because it's trapped within your quiz method.

import script

script.quiz()
    ...
    class_num = input('class: ')
    # quiz function exits, making class_num go out of scope

# since class_num doesn't exist, this NameError
def path(class_num):
    ... 

If you want the class name to be usable outside the function, return it

def quiz()
    ...
    ...
    num1, num2 = num()   
    name = input('name: ')
    class_num = input('class: ')
    return class_num  # <=======

And in the other script

class_num = script.quiz()  # <=======

def path(class_num):
    ... 

If you want the other variables from quiz to be available, you have several options:

  • Multiple returns, as you did for your num method
  • Create something to that holds all your data which counts as a single return. An object would be perfect, other options would be a namedtuple or a dictionary
  • Use globals (please don't do this)
Sam Myers
  • 2,112
  • 2
  • 15
  • 13
0

I modified the script to show both: a) return value as mentioned by others, b) missing execution of path, with the returned value as parameter value. Here we have script.py

def num():
    num1 = 1
    num2 = 2
    return num1, num2

def quiz():
   num1, num2 = num()
   name = "Andreas"
   class_num = 9
   return class_num

if __name__ == '__main__':
    quiz()

And here something.py

import script

def path(class_num):
    pass
    # do stuff

class_n = script.quiz()
path(class_n)

I hope this can show how it could work. You possibly used the variable class_num somewhere later outside the function path. That gives you a NameError.

Andreas Müller
  • 210
  • 2
  • 10