简体   繁体   中英

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. That being said, it is generally good practice to avoid the usage of the global keyword whenever possible.

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. However, in COUNTDOWN, because you are not rebinding the names and are only accessing the values bound to those names, global is not necessary.

For more information look here: Use of "global" keyword in Python

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:

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. If we were to instead, USE eg 's content to do something different we would do this:

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'. Then we have declared eg2 to be the same as eg , but we have not changed eg

If you wanted to then use the outcome of the function example, you would add a 'return' line.

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.

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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