繁体   English   中英

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

[英]calling a variable from another function in python 3.3?

我知道这已经被问过了,但是我无法终生理解。 我正在尝试创建一个简单的程序来获取两个日期,并且计数显示它们之间还剩下多少天。

这是我当前的代码:

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()

编辑:

抱歉,我没意识到没有缩进。

编辑:

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

python中的global关键字用于在本地上下文中重新绑定全局变量。 话虽如此,通常最好避免在任何可能的情况下使用global关键字。

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

有关更多信息,请参见此处: 在Python中使用“ global”关键字

我只是使您的代码可行,如下所示:

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()

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

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

def function():
    global variable

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

仅当您要更改变量时才使用此变量,而不仅仅是使用变量内部的变量。 例如,

def example():
    global eg
    eg = 1

这里我们使用global是因为我们正在更改eg的内容。 如果我们相反,使用eg的内容做不同的事情,我们这样做:

def example(eg):
    eg2 = eg

我们在这里说,“我想使用eg包含的值,但我不想更改它”。 然后我们声明eg2eg相同,但是我们没有更改eg

如果要使用函数示例的结果,则可以添加“ return”行。

def example(eg):
    eg2 = eg
    return eg

那么我们将像这样调用该函数:

eg3 = example(eg)

这会将示例的结果放入eg3中。

希望这可以帮助 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM