By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Creating a menu based program based on a list

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1. #Main
  2. def main():
  3.     array = [] #this would be the list, but what do i do with it?
  4.     choice = displayMenu()
  5.     while choice != '4':
  6.         if choice == '1':
  7.             addNum()
  8.         if choice == '2':
  9.             calcMean()
  10.         if choice == '3':
  11.             displayMedian()
  12.         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.

Expand|Select|Wrap|Line Numbers
  1. #adding numbers to the list
  2. def addNum(): #how do I get it to return to the list in main?
  3.     number = raw_input("Enter number: ")
  4.     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
Nov 7 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 621
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.
Expand|Select|Wrap|Line Numbers
  1. ##   see the Python style guide
  2. ##   function and variable names are all lower case
  3. def add_num(array): #how do I get it to return to the list in main?
  4.     number = raw_input("Enter number: ")
  5.     array.append(number)
  6.     return array
  8. ##   calling main() from "if __name__ == '__main__' is redundant (unnecessary)
  9. ##   and considered bad style as it is not obvious that this code runs when you
  10. ##   run this program (as opposed to executing a function from another program).
  11. if __name__ == '__main__:
  12.     array = [] #this would be the list, but what do i do with it?
  13.     choice = displayMenu()
  14.     while choice != '4':
  15.         if choice == '1':
  16.             array = add_num(array)
  17.             print array
  18.         if choice == '2':
  19.             calcMean()
  20.         if choice == '3':
  21.             displayMedian()
  22.         choice = displayMenu() 
Nov 7 '10 #2

P: 2
Thank you very much. I think i was just so stressed from everything else that I missed something so simple.
Nov 7 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.