python program prints a variable then says its not defined.why? (closed)
Question:
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?
Answers:
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
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?
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