I declared variable t1 outside the function but was able to use it without using global keyword but for counta and counto I have to use global why? While using counta and counto without global I'm getting the error : local variable 'counta' referenced before assignment
def countApplesAndOranges(s, t, a, b, apples, oranges):
    counta=0
    counto=0
    for x in range(len(apples)):
        app=a+apples[x]
    
        if app in range(s,t1):
            #global counta
            counta+=1
    for y in range(len(oranges)):
        org=b+oranges[y]
        if org in range (s,t1):
            #global count
            counto+=1
    print(counta,counto, sep="\n")
        
if __name__ == '__main__':
    first_multiple_input = list(map(int,input().rstrip().split()))
    s = first_multiple_input[0]
    t = first_multiple_input[1]
    second_multiple_input = input().rstrip().split()
    a = int(second_multiple_input[0])
    b = int(second_multiple_input[1])
    third_multiple_input = input().rstrip().split()
    m = int(third_multiple_input[0])
    n = int(third_multiple_input[1])
    apples = list(map(int, input().rstrip().split()))
    oranges = list(map(int, input().rstrip().split()))
    from datetime import datetime
    start_time = datetime.now()
    t1=t+1
    countApplesAndOranges(s, t, a, b, apples, oranges)
    end_time=datetime.now()
    duration='Duration:{}'
    print(duration.format(end_time-start_time))
