0

I am running into an issue with returning the month and day variables to use in other functions.

def date():
    date = raw_input("Date (ex. Jun 19): ")
    date = date.split(' ')
    month = date[0]
    month = month[:3].title()
    day = date[1]
    return (month, day)

def clone(month,day):
    print month day

Here is the output for the script:

Date (ex. Jun 19): june 19
Traceback (most recent call last):
  File "./manualVirt.py", line 26, in <module>
    main()
  File "./manualVirt.py", line 12, in main
    clone(agent,month,day)
NameError: global name 'month' is not defined
4
  • clone(agent,month,day) isn't in the code you have shown us. Is there more? Commented Jun 26, 2013 at 8:12
  • Haidro, sorry about the "agent" part. that is not a factor in this. I just didn't remove it from the text. Commented Jun 26, 2013 at 8:14
  • You should either use global variables, or wrap these functions together into a class. That, or feed one functions return values into the other. Commented Jun 26, 2013 at 8:15
  • Please show how you use your functions. Commented Jun 26, 2013 at 8:18

4 Answers 4

1

Since you're returning a tuple from date() I will be assuming that this would be the thing you want to do

month_day = date()
clone(month_day[0], month_day[1])

And also the following line in clone()

print month day

should be

print month, day
Sign up to request clarification or add additional context in comments.

Comments

1

Is it possible that You want to pass result of one function into another ?

month, day = date()
clone(month, day)

or You can unpack function result during passing it into second one

result = date()
clone(*result)

or even

clone(*date())

1 Comment

Don't you mean month, day = date() ?
0

You're probably wondering how to use a variable in the global space when it is declared in a local space. use global:

def myfunc():
    global a
    a = 5

print a
# NameError: name 'a' is not defined
myfunc()
print a
# 5

3 Comments

@GregRobertDutertre Global variables should normally be avoided as much as possible.
@JoachimPileborg That is no reason that this answer is incorrect.
@JoachimPileborg I do hope you mean the global statement ;). But anyway, in this circumstance surely there is no harm.
0

I think the problem comes from here: print month day.

You need to separate the arguments by commas if you want to print multiple things:

print month, day

4 Comments

The error is a NameError, not a SyntaxError, so this is probably some error in formatting the question
@Haidro no, you can do print X, Y in Python 2 just like you can do print(X,Y) in Python 3.
@InbarRose arg, sorry, I meant in my comment "That would otherwise raise a SyntaxError, not a NameError
@Haidro undoubtedly, this is not the correct solution, but I was just commenting that what your statement appeared to be was incorrect.

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.