Python全局变量回调和定义错误

时间:2015-04-09 02:13:07

标签: python reference global-variables

我正在编写以下代码。当我尝试在每个if / elif缩进中使用全局变量时,我收到一个错误,指出该变量在变为全局之前已被使用。如果我只在第一个if中使用全局,我会收到一个错误,指的是无法在本地编辑局部变量这一事实。

以下代码是我能够正常工作的最远的代码。调试时,此代码不会导致任何错误,只有运行时的回调错误。

global daycountone
global daycounttwo
global y
print("Input date using numbers only")
print("Ex. Day: 13")
print("    Month: 4")
print("    Year: 2009")
dayone = input("Day ")
monthone = input("Month ")
yearone = input("Year ")
print("Input another date, still using numbers only")
print("Ex. Day: 13")
print("    Month: 4")
print("    Year: 2009")
daytwo = input("Day: ")
monthtwo = input("Month: ")
yeartwo = input("Year: ")
y = yearone
while(y>0):
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 366
                        else:
                                daycountone = daycountone + 365                 
                else:
                        daycountone = daycountone + 365
        else:
                daycountone = daycountone + 366
        y = y-1
while(y>0):
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 366
                        else:
                                daycounttwo = daycounttwo + 365                 
                else:
                                daycounttwo = daycounttwo + 365
        else:
                daycounttwo = daycounttwo + 366
        y = y-1
if(monthone == 2):
        daycountone = daycountone + 31
elif(monthone == 3):
        daycountone = daycountone + 59
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1           
elif(monthone == 4):
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1   
        daycountone = daycountone + 90
elif(monthone == 5):
        daycountone = daycountone + 120
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1
elif(monthone == 6):
        daycountone = daycountone + 151
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1
elif(monthone == 7):
        daycountone = daycountone + 181
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1   
elif(monthone == 8):
        daycountone = daycountone + 212
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1   
elif(monthone == 9):
        daycountone = daycountone + 243
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1   
elif(monthone == 10):
        daycountone = daycountone + 273
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1   
elif(monthone == 11):
        daycountone = daycountone + 304
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1   
elif(monthone == 12):
        daycountone = daycountone + 334
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1
if(monthtwo == 2):
        daycounttwo = daycounttwo + 31
elif(monthtwo == 3):
        daycounttwo = daycounttwo + 59
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1           
elif(monthtwo == 4):
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1   
        daycounttwo = daycounttwo + 90
elif(monthtwo == 5):
        daycounttwo = daycounttwo + 120
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1   
elif(monthtwo == 6):
        daycounttwo = daycounttwo + 151
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycountone = daycountone + 1
                else:
                        daycountone = daycountone + 1
elif(monthtwo == 7):
        daycounttwo = daycounttwo + 181
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1   
elif(monthtwo == 8):
        daycounttwo = daycounttwo + 212
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1   
elif(monthtwo == 9):
        daycounttwo = daycounttwo + 243
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1   
elif(monthtwo == 10):
        daycounttwo = daycounttwo + 273
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1   
elif(monthtwo == 11):
        daycounttwo = daycounttwo + 304
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1   
elif(monthtwo == 12):
        daycounttwo = daycounttwo + 334
        if(y%4 == 0):
                if(y%100 == 0):
                        if(y%400 == 0):
                                daycounttwo = daycounttwo + 1
                else:
                        daycounttwo = daycounttwo + 1
daycountone = daycountone + dayone
daycounttwo = daycounttwo + daytwo
daysbetween = daycounttwo - daycountone
if(daysbetween<0):
        daysbetween = -1 * daysbetween
prnt = 'The number of days between the two dates is ' + daysbetween + 'days exactly, accounting for leap years'
print prnt

2 个答案:

答案 0 :(得分:0)

Python基本上遵循功能范围:请参阅Short Description of the Scoping Rules?

因为你没有函数,所以你不需要全局变量。当您确实需要使用全局变量时,可以在第一次读取的本地范围内声明它们。例如:

SOME_COUNTER = 0

def some_func(some_num):
    global SOME_COUNTER
    SOME_COUNTER += some_num

some_func(3)
print SOME_COUNTER  # 3
some_func(2)        
print SOME_COUNTER  # 5

def some_other_func(some_num):
    SOME_COUNTER += some_num  # this won't work; SOME_COUNTER isn't in scope

some_other_func(3)  # blows up with an UnboundLocalError saying SOME_COUNTER was used before definition (i.e. it isn't in scope)

答案 1 :(得分:0)

全局变量可以让你修改函数中的一个变量,但因为你没有使用任何函数,所以你不需要做任何全局变量。

使用datetime module

可以更轻松地完成此操作