2 Replies - 1086 Views - Last Post: 31 May 2013 - 09:25 PM Rate Topic: -----

#1 tom.h   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 31-May 13

Using python variables from another file.

Posted 31 May 2013 - 11:52 AM

Hi guys I am pretty new to programming and have been working on this python program. I have been having problems using variables from other python files.

My code:

getname.py

def givename():                      
    print
    print "What is your name?"
    print
    name = raw_input(">> ")
    if len(name) < 3:
        print "Your name is to short. It must be more then 3 characters."
        givename()
    else:
        print
        print "Welcome "+name+"."
        print



main.py

import os
import getname

def askname():
    getname.givename()
askname()

def menu():
    command = raw_input(">> ")
    input = command.split()
    print input
    if input[0] == "Who":
        print name
    else:
        print "failure"
    menu()
menu()




Basically I want the user to be able to type "Who" and receive the name they typed. Everything else seems to be working except when I do type "Who" it gives me this error:

Traceback (most recent call last):
  File "C:\Users\TOM\Desktop\game.py", line 17, in <module>
    menu()
  File "C:\Users\TOM\Desktop\game.py", line 16, in menu
    menu()
  File "C:\Users\TOM\Desktop\game.py", line 13, in menu
    print name
NameError: global name 'name' is not defined


I did import the file getname in my main file so shouldn't the variables be usable from that file?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Using python variables from another file.

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6833
  • View blog
  • Posts: 28,342
  • Joined: 12-December 12

Re: Using python variables from another file.

Posted 31 May 2013 - 12:55 PM

Quote

NameError: global name 'name' is not defined

As the error message states, name is not available globally, as it is a local variable in the givename() function.

You could make it global but it is FAR BETTER that you modify your function to return the name.

return: the hard way

This post has been edited by andrewsw: 31 May 2013 - 12:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 tom.h   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 31-May 13

Re: Using python variables from another file.

Posted 31 May 2013 - 09:25 PM

View Postandrewsw, on 31 May 2013 - 12:55 PM, said:

Quote

NameError: global name 'name' is not defined

As the error message states, name is not available globally, as it is a local variable in the givename() function.

You could make it global but it is FAR BETTER that you modify your function to return the name.

return: the hard way


Thanks I got it eventually working by putting a global variable for name in getname.py I am going to look at a couple tutorials on return and try to implement that though.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1