从python 3.3中的另一个函数调用变量?

时间:2013-12-14 02:18:12

标签: python python-3.x

我知道以前曾经问过,但我不能理解它的生活。我正在尝试创建一个获取两个日期的简单程序,计数显示它们之间剩余的天数。

这是我目前的代码:

month = 0
day = 0
year = 0

def getDate(): #gets the current date
    global month
    global day
    global year
    print( 'What is the current month?' )
    month = month + int(input())
    print( 'What is the current day?' )
    day = day + int(input())
    print( 'What is the current year?' )
    year = year + int(input())
    print( 'The current date is ' + str(month) + '/' + str(day) + '/' + str(year) + '. Is this correct?' )
    YESNO = input() #confirms date
    if YESNO == 'YES' or YESNO == 'yes':
        print( 'Okay.' )
    else:
        getDate()

    newMonth = 0
    newDay = 0
    newYear = 0

def newDate(): #gets the desired countdown date
    global newMonth
    global newDay
    global newYear

    print( 'What is the desired countdown month?' )
    newMonth = newMonth + int(input())
    print( 'What is the desired countdown day?' )
    newDay = newDay + int(input())
    print( 'What is the desired countdown year?' )
    newYear = newYear + int(input())
    print( 'The desired countdown date is ' + str(newMonth) + '/' + str(newDay) + '/' + str(newYear) + '. Is this correct?' )
    YESNO = input() #confirms date
    if YESNO == 'YES' or YESNO == 'yes':
        print( 'Okay.' )
    else:
        newDate()

def COUNTDOWN(): #prints countdown
    global newMonth
    global newDay
    global newYear

    global month
    global day
    global year

    if newMonth < Month:
        countDownMonth = int(Month) - int(newMonth)
    else:
        countDownMonth = int(newMonth) - int(Month)
    if newDay < Day:
        countDownDay = int(Day) - int(newDay)
    else:
        countDownDay = int(newDay) - int(Day)
    if newMonth < Year:
        countDownYear = int(Year) - int(newYear)
    else:
        countDownYear = int(newYear) - int(Year)
    print( countDownMonth + '/' + countDownDay + '/' + countDownYear )

getDate()
newDate()
COUNTDOWN()

编辑:

我道歉,我没有意识到它没有缩进。

编辑:

我的问题是如何创建交叉函数变量?

3 个答案:

答案 0 :(得分:1)

python中的global关键字用于在本地上下文中重新绑定全局变量。话虽如此,通常的做法是尽可能避免使用全局关键字。

在您发布的代码中,有必要在函数getDate和newDate中使用global,以便在全局环境中绑定这些名称。但是,在COUNTDOWN中,因为您没有重新绑定名称并且只访问绑定到这些名称的值,所以不需要全局。

有关详细信息,请查看此处:Use of "global" keyword in Python

答案 1 :(得分:0)

我只是让您的代码可以使用,如下所示:

month = 0
day = 0
year = 0
newMonth = 0
newDay = 0
newYear = 0

def getDate(): #gets the current date
    global month
    global day
    global year

    print( 'What is the current month?' )
    month = month + int(input())
    print( 'What is the current day?' )
    day = day + int(input())
    print( 'What is the current year?' )
    year = year + int(input())
    print( 'The current date is ' + str(month) + '/' + str(day) + '/' + str(year) + '. Is this correct?' )
    YESNO = raw_input() #confirms date
    print YESNO
    if YESNO == 'YES' or YESNO == 'yes':
        print( 'Okay.' )
    else:
        getDate()

def newDate(): #gets the desired countdown date
    global newMonth
    global newDay
    global newYear

    print( 'What is the desired countdown month?' )
    newMonth = newMonth + int(input())
    print( 'What is the desired countdown day?' )
    newDay = newDay + int(input())
    print( 'What is the desired countdown year?' )
    newYear = newYear + int(input())
    print( 'The desired countdown date is ' + str(newMonth) + '/' + str(newDay) + '/' + str(newYear) + '. Is this correct?' )
    YESNO = raw_input() #confirms date
    if YESNO == 'YES' or YESNO == 'yes':
        print( 'Okay.' )
    else:
        newDate()

def COUNTDOWN(): #prints countdown
    global newMonth
    global newDay
    global newYear
    global month
    global day
    global year

    if newMonth < month:
        countDownMonth = int(month) - int(newMonth)
    else:
        countDownMonth = int(newMonth) - int(month)
    if newDay < day:
        countDownDay = int(day) - int(newDay)
    else:
        countDownDay = int(newDay) - int(day)
    if newMonth < year:
        countDownYear = int(year) - int(newYear)
    else:
        countDownYear = int(newYear) - int(year)
    print( str(countDownMonth) + '/' + str(countDownDay) + '/' + str(countDownYear) )

getDate()
newDate()
COUNTDOWN()

在我的环境中,此代码正常运行,但我不确定输出是否正确。

答案 2 :(得分:0)

使用global关键字,如下所示:

def function():
    global variable

这基本上说,我想访问变量,即使我知道它是全局的,我仍然想要它。

如果您正在更改变量,请仅使用此项,而不仅仅是使用其中的任何内容。 例如,

def example():
    global eg
    eg = 1

我们在这里使用全局,因为我们正在更改eg的内容。如果我们改为使用eg的内容做一些不同的事情,我们会这样做:

def example(eg):
    eg2 = eg

我们在这里说'我想使用eg包含的值,但我不想改变它'。 然后我们宣布eg2eg相同,但我们没有更改eg

如果你想使用函数示例的结果,你可以添加一个'return'行。

def example(eg):
    eg2 = eg
    return eg

然后我们会像这样调用函数:

eg3 = example(eg)

这将示例的结果放入eg3。

希望这会有所帮助:)