Here is the code:
def rand():
import random
num = (random.randint(1000,9999))
print(num)
rand()
print(num)
Within the subroutine, the variable "num" is printed successfully. However, outside of it, when printing is attempted, the following error message is displayed:
Traceback (most recent call last):
File "main.py", line 8, in <module>
print(num)
NameError: name 'num' is not defined
Why does this happen, and what are the ways I can work around it?