13
I've generally heard that using global variables is a bad practice when programming. So are there any alternatives to what I'm trying to do here, which is to use the variable my_array from inside another function?
import random
def main():
create_list_and_find_max_and_min(10)
the_smart_way()
def create_list_and_find_max_and_min(n):
global my_array
my_array = []
n = input("How many numbers do you want in your array?:")
for i in range(n):
my_array.append(random.randint(1,n))
print "My array is:", my_array
min = my_array[0]
for number in my_array:
if min > number:
min = number
print "The minimum value in the array is:", min
max = my_array[0]
for number in my_array:
if max < number:
max = number
print "The maximum value in the array is:", max
def the_smart_way():
# "This one uses the built-in Python functions for min/max..."
min_my_array = min(my_array)
max_my_array = max(my_array)
return min_my_array, max_my_array
if __name__ == '__main__':
main()