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?

4
  • Do both files reside on the same location?..I mean on the same folder Commented Feb 11, 2016 at 18:53
  • Yep they do. They're both in the same folder Commented Feb 11, 2016 at 18:54
  • 1
    @Matt You asked a very similar question earlier today. Have you read the answers there? Commented Feb 11, 2016 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! Commented Feb 12, 2016 at 9:24

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

How would I parse results?
it will be a regular python list, so something like 'for result in results:' will let you iterate/loop over the individual elements.
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 !

2 Comments

What if I also want to use name, num1, num2 etc.? Do I have to repeat the same thing?
you can just return all of them through a list through return statement like this return [num1,num2,num3]
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):

3 Comments

{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.
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 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
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)

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.