-1
print ("hello world")


x = input ("we are starting the count from >> ")
x = int  (x)  #makes the input string of x to int


y = input ("we are ending our count at >> ")
y = int (y)  #makes the input strint of y to int


def deleting_extars():
    l = ( (x*(x-1))/ 2)
    l = int (l)
    pass

deleting_extars()

def make_the_others_count():
     p = ((y*(y-1))/2)     p = int (p)
     print (p)
     pass
make_the_others_count()

def final_func():
    print (p-l)


final_func()

now if i run the program u can see it prints "p" in the terminal the output

$python python.py
hello world
we are starting the count from >> 4
we are ending our count at >> 55
1485
Traceback (most recent call last):
  File "/home/moula/x/python.py", line 30, in <module>
    final_func()
  File "/home/moula/x/python.py", line 27, in final_func
    print (p-l)
NameError: name 'p' is not defined

we can see it prints "p" but then it says p is not defined "p" is inside a function so is "l" but "l" works fine why "p" does not? source code: https://github.com/Golam-moula/-x/blob/master/python.py

how can i do what i wanted?

Golam Moula
  • 83
  • 2
  • 7
  • 2
    You define `p` in `make_the_others_count` and then print it. I variable defined within a function is local to that function and only available inside it. `final_func` is a different function and doesn't have access to `p`. You could return `p` from `make_the_others_count` and then pass it in to `final_func` as an argument. – Kemp Jul 01 '21 at 10:52
  • p is not global so it will not be accessible in second function – Vikas Mulaje Jul 01 '21 at 10:53
  • `l` (bad choice of single letter variable btw, it might look like an I as well - and actually does...) isn't defined either, but the first error encountered was with `p`. – Thierry Lathuille Jul 01 '21 at 10:53
  • 3
    [python - Short description of the scoping rules?](https://stackoverflow.com/q/291978/3890632) – khelwood Jul 01 '21 at 10:53

1 Answers1

2

the variable p is in a local area of the function where it was declared, you can declare it outside or return it in a variable and use that variable