# 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!
random_number = generate_random(min, max)
Variables assigned inside a function don’t exist outside the function. Programming would be very difficult if function variables were global. Writing a new function would require checking every variable name against every other variable in all other functions to avoid clashing. Creating a local namespace while executing inside a function greatly limits the possibility of name clashes, but they still happen. For example, min and max are the names of built-if functions. Using min and max as variable names prevents you from using those functions.
You can use the “global” declaration inside a function to tell python “the following variables are in the global namespace”. Using “global random_number” inside the generate_random() function would fix your problem, but having the function “return” a variable is a better choice.
Apologies Python for using function names as variable names.
So when I say
print(generate_random(minimum, maximum))
the interpreter/compiler first evaluates the generate_random function. The value that it returns is passed to the print command. If the print command/function is not instructed to be a
composite function then the value returned by the defined function generate_random is
lost 
A value is always returned. In your second example you chose to ignore it.
Functions always have a value, even if they don't have a return statement. Functions without a return statement have the value None.
Also you should not use
min
and
max
as variable names.
These name are used bye Python.
>>> min
<built-in function min>
>>> help(min)
Help on built-in function min in module builtins:
min(...)
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
Can make it like this,then have no global variables.
import random
def generate_random(min_numb, max_numb):
random_number = random.randint(min_numb, max_numb)
return random_number
def rand_result():
reply = "y"
while reply.lower() == "y":
min_numb = int(input("Enter the min_numbimum for a random number: "))
max_numb = int(input("Enter the max_numbimum for a random number: "))
print(generate_random(min_numb, max_numb))
reply = input("Continue(y/n): ")
if __name__ == '__main__':
rand_result()
@
snippsat
When you say Python discourages
global variables does that mean if you do have such a thing it could become problematic for large programs because we'd have to keep checking if a variable name has already been used/not? The way I see it, a variable defined within a function has no "life" outside the function (as the error messages I listed above show).
Suppose I define a function as below:
def summation(addend_1, addend_2):
total = addend_1 + addend_2
return total
and then call it ...
addend_1 = 3
addend_2 = 4
summation(addend_1, addend_2)
How can access the value in the variable total?
I know I can do this:
print(summation(addend_1, addend_2))
but not this:
summation(addend_1, addend_2)
print(total)
(Aug-24-2024, 12:49 PM)Hudjefa Wrote: [ -> ]How can access the value in the variable total?
You assign to variable same name or a new name a of choice.
return
just return the value and close the function.
So now have option to just
print()
value or assign to variable(may need to eg do som more calculation).
So here with type hints this is return vaule
-> int
,so now see that function is return an integer.
def summation(addend_1: int, addend_2: int) -> int:
total = addend_1 + addend_2
return total
def multiply(value: int) -> int:
result = value * 2
return result
addend_1 = 3
addend_2 = 4
sum_total = summation(addend_1, addend_2)
result = multiply(sum_total)
print(result)
Output:
14