0

我遇到了返回月份和日期变量以在其他函数中使用的问题。

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

这是脚本的输出:

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

4 回答 4

1

既然你要从我那里回来tupledate()我会假设这就是你想做的事情

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

还有以下行clone()

print month day

应该

print month, day
于 2013-06-26T08:17:57.207 回答
1

您是否有可能想将一个函数的结果传递给另一个函数?

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

或者您可以在将函数结果传递到第二个时解压

result = date()
clone(*result)

甚至

clone(*date())
于 2013-06-26T08:19:52.310 回答
0

您可能想知道在局部空间中声明变量时如何在全局空间中使用它。使用global

def myfunc():
    global a
    a = 5

print a
# NameError: name 'a' is not defined
myfunc()
print a
# 5
于 2013-06-26T08:13:41.243 回答
0

我认为问题来自这里:print month day.

如果要打印多个内容,则需要用逗号分隔参数:

print month, day
于 2013-06-26T08:15:08.737 回答