简体   繁体   English

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

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

I know that this has been asked before, but I cant for the life of me understand it. 我知道这已经被问过了,但是我无法终生理解。 I'm trying to create a simple program that gets two dates, and counts shows how many days are left between them. 我正在尝试创建一个简单的程序来获取两个日期,并且计数显示它们之间还剩下多少天。

This is my current code: 这是我当前的代码:

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

EDIT: 编辑:

I apologize, I didn't realize it wasn't indented. 抱歉,我没意识到没有缩进。

EDIT: 编辑:

My question is how do I create a cross-function variable? 我的问题是如何创建跨函数变量?

The global keyword in python is used to rebind a global variable in a local context. python中的global关键字用于在本地上下文中重新绑定全局变量。 That being said, it is generally good practice to avoid the usage of the global keyword whenever possible. 话虽如此,通常最好避免在任何可能的情况下使用global关键字。

In the code that you posted, it is necessary to use global in the functions getDate and newDate in order to bind those names in the global environment. 在发布的代码中,有必要在getDate和newDate函数中使用global,以便在全局环境中绑定这些名称。 However, in COUNTDOWN, because you are not rebinding the names and are only accessing the values bound to those names, global is not necessary. 但是,在COUNTDOWN中,因为您没有重新绑定名称,而仅访问绑定到这些名称的值,所以不需要全局。

For more information look here: Use of "global" keyword in Python 有关更多信息,请参见此处: 在Python中使用“ global”关键字

I just make your code workable as below: 我只是使您的代码可行,如下所示:

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

In my env, this code is working, but i am not sure whether the output correct or not. 在我的环境中,此代码有效,但是我不确定输出是否正确。

use the global keyword, like so: 使用global关键字,如下所示:

def function():
    global variable

this basically says, I want to access variable, even though I know it is global I still want it anyway. 这基本上是说,我想访问变量,即使我知道它是全局变量,我还是想要它。

Only use this if you are Changing the variable, not just using whatever is inside it. 仅当您要更改变量时才使用此变量,而不仅仅是使用变量内部的变量。 eg, 例如,

def example():
    global eg
    eg = 1

Here we use global because we are changing eg 's content. 这里我们使用global是因为我们正在更改eg的内容。 If we were to instead, USE eg 's content to do something different we would do this: 如果我们相反,使用eg的内容做不同的事情,我们这样做:

def example(eg):
    eg2 = eg

Here we are saying, 'I want to use the value that eg contains, but I dont want to change it'. 我们在这里说,“我想使用eg包含的值,但我不想更改它”。 Then we have declared eg2 to be the same as eg , but we have not changed eg 然后我们声明eg2eg相同,但是我们没有更改eg

If you wanted to then use the outcome of the function example, you would add a 'return' line. 如果要使用函数示例的结果,则可以添加“ return”行。

def example(eg):
    eg2 = eg
    return eg

then we would call the function like so: 那么我们将像这样调用该函数:

eg3 = example(eg)

This puts the result of example into eg3. 这会将示例的结果放入eg3中。

Hope this helps :) 希望这可以帮助 :)

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

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