Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8237373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:27:03+00:00 2026-06-07T19:27:03+00:00

I’ve generally heard that using global variables is a bad practice when programming. So

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:27:05+00:00Added an answer on June 7, 2026 at 7:27 pm

    Yes, there are two alternatives.

    First, you can pass the values around instead of using globals. For example, create_list_and_find_max_and_min can create the array locally and return it, then you can pass it in to the_smart_way:

    import random  
    
    def main():  
        my_array = create_list_and_find_max_and_min(10)  
        print the_smart_way(my_array)
    
    def create_list_and_find_max_and_min(n):
        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
        return my_array
    
    def the_smart_way(my_array):
        # "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()
    

    Second, you can create a class that encapsulates the data and the functions that operate on that data:

    import random  
    
    class MyArrayClass(object):
        def create_list_and_find_max_and_min(self, n):
            self.my_array = []
            n = input("How many numbers do you want in your array?:")
            for i in range(n):
                self.my_array.append(random.randint(1,n))
            print "My array is:", self.my_array
    
            min = self.my_array[0]
            for number in self.my_array:
                if min > number:
                    min = number
            print "The minimum value in the array is:", min
    
            max = self.my_array[0]
            for number in self.my_array:
                if max < number:
                    max = number
            print "The maximum value in the array is:", max
    
        def the_smart_way(self):
            # "This one uses the built-in Python functions for min/max..."
            min_my_array = min(self.my_array)
            max_my_array = max(self.my_array)
            return min_my_array, max_my_array
    
    def main():
        my_array = MyArrayClass()
        my_array.create_list_and_find_max_and_min(10)  
        print my_array.the_smart_way()
    
    if __name__ == '__main__':
        main()
    

    You should probably understand the reasons global variables are bad practice.

    Imagine that you want to create two arrays. With global variables, the second one will replace the first one, which will be gone forever.

    create_list_and_fix_max_and_min(10)
    create_list_and_fix_max_and_min(20)
    # No way to operate on the original array!
    

    With a local variable, you can store both of them:

    my_array_1 = create_list_and_fix_max_and_min(10)
    my_array_2 = create_list_and_fix_max_and_min(20)
    the_smart_way(my_array_1)
    

    Using an object provides the same benefit; the difference between the two ultimately comes down to whether the operations are part of the meaning of the data, or whether the data stand alone and the operations are generic. (Or, sometimes, whether you’re more of a functional snob or an OO snob…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.