Creating a menu based program based on a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anne Finch
    New Member
    • Nov 2010
    • 2

    Creating a menu based program based on a list

    I am having issues creating a menu-driven program that should accept integers and calculate the mean and median values and display them. I'm not having trouble calculating the Mean or Median, but I am having trouble with configuring the list into the program.

    Code:
    #Main
    def main():
        array = [] #this would be the list, but what do i do with it?
        choice = displayMenu()
        while choice != '4':
            if choice == '1':
                addNum()
            if choice == '2':
                calcMean()
            if choice == '3':
                displayMedian()
            choice = displayMenu()
    Now, this is my main function. I'm not completely sure what to do with the list. The next part would be the displayMenu() function, but that's correct so I'm not going to post it. However, next would be option number one, the addNum() function.

    Code:
    #adding numbers to the list
    def addNum(): #how do I get it to return to the list in main?
        number = raw_input("Enter number: ")
        array.append(number)
    I feel like I'm close to getting this correct, but at the same time I'm completely stuck and don't know what to do.

    Any help, advice, and input would be greatly appreciated. thank you
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    I am having trouble with configuring the list into the program.
    I have no idea what this means, but generally speaking, you haven't returned any thing from the function. You have to return a variable from a function as, any changes that are made in the function stay in the function, to alter the Vegas phrase, unless you return the value to some variable outside the function. This is very basic stuff, which you should know, so instead of coding yourself start with one of the online books here or here.
    Code:
    ##   see the Python style guide http://www.python.org/dev/peps/pep-0008/
    ##   function and variable names are all lower case
    def add_num(array): #how do I get it to return to the list in main?
        number = raw_input("Enter number: ")
        array.append(number)
        return array
    
    ##   calling main() from "if __name__ == '__main__' is redundant (unnecessary)
    ##   and considered bad style as it is not obvious that this code runs when you
    ##   run this program (as opposed to executing a function from another program).
    if __name__ == '__main__:
        array = [] #this would be the list, but what do i do with it?
        choice = displayMenu()
        while choice != '4':
            if choice == '1':
                array = add_num(array)
                print array
            if choice == '2':
                calcMean()
            if choice == '3':
                displayMedian()
            choice = displayMenu()

    Comment

    • Anne Finch
      New Member
      • Nov 2010
      • 2

      #3
      Thank you very much. I think i was just so stressed from everything else that I missed something so simple.

      Comment

      Working...