# Random number generator function import random reply = "y" # Defining the generate_random function def generate_random(min, max): random_number = random.randint(min, max) return random_number # Using the newly definted function generate_random while reply == "y": min = int(input("Enter the minimum for a random number: ")) max = int(input("Enter the maximum for a random number: ")) print(generate_random(min, max)) reply = input("Continue(y/n): ")The simple random number generator program above works.
My issue is the following doesn't work:
# Random number generator function import random reply = "y" # Defining the generate_random function def generate_random(min, max): random_number = random.randint(min, max) return random_number # Using the newly definted function generate_random while reply == "y": min = int(input("Enter the minimum for a random number: ")) max = int(input("Enter the maximum for a random number: ")) generate_random(min, max) print(random_number) reply = input("Continue(y/n): ")I'm posting the error I get below:
Error:Enter the minimum for a random number: 37
Enter the maximum for a random number: 98
Traceback (most recent call last):
File "main.py", line 17, in <module>
print(random_number)
^^^^^^^^^^^^^
NameError: name 'random_number' is not defined
When I do define the variable random_number (here I did random_number = 3) the following happens: # Random number generator function import random reply = "y" random_number = 3 # Defining the generate_random function def generate_random(min, max): random_number = random.randint(min, max) return random_number # Using the newly definted function generate_random while reply == "y": min = int(input("Enter the minimum for a random number: ")) max = int(input("Enter the maximum for a random number: ")) generate_random(min, max) print(random_number) reply = input("Continue(y/n): ")
Output:Enter the minimum for a random number: 27
Enter the maximum for a random number: 98
3
Continue(y/n):
The function either doesn't run or if it runs the value is not passed onto the variable random_number.It's as if the print function is locked to the function I've defined.
Mayday! Mayday!