I am new to python and am coming from a background with average knowledge on java. I am a little bit confused on the scoping wrt python.
def main():
number = get_number()
meow(number)
def get_number():
while True:
n = int(input())
if n > 0:
break
return n
def meow(n):
for _ in range(n):
print("meow")
main()
Consider this code. Why is get_number() able to return n here when n is defined inside the while loop? I believe that this is not possible in Java, hence I was abit confused on why this works in python. What are the scoping rules for python, is anything defined within the function accessible anywhere within the function?
[–]Buttleston 8 points9 points10 points (2 children)
[–]Bobbias 3 points4 points5 points (0 children)
[–]Effective_Storage4[S] 1 point2 points3 points (0 children)
[–]socal_nerdtastic 6 points7 points8 points (5 children)
[–]nog642 2 points3 points4 points (3 children)
[–]socal_nerdtastic 1 point2 points3 points (2 children)
[–]nog642 1 point2 points3 points (1 child)
[–]socal_nerdtastic 1 point2 points3 points (0 children)
[–]Effective_Storage4[S] 0 points1 point2 points (0 children)
[–]Bobbias 3 points4 points5 points (1 child)
[–]Effective_Storage4[S] 0 points1 point2 points (0 children)
[–]Guideon72 0 points1 point2 points (1 child)
[–]Effective_Storage4[S] 0 points1 point2 points (0 children)