Advertisement
a1ananth

Test - Classes.py

Oct 21st, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. class Functions:
  2.     def __init__(self):
  3.         pass
  4.        
  5.     def showMenu(self):         # prints the menu
  6.         print("""
  7.        \n\t1 - Add 2 numbers\n\n
  8.        2 - Add n numbers\n\n
  9.        3 - Take average of n numbers\n\n
  10.        4 - Show menu again\n\n
  11.        5 - Exit\n
  12.        """)
  13.    
  14.     def getChoice(self):                # read user's choice
  15.         self.showMenu()
  16.         choice = raw_input("Your Choice : ")
  17.         choice = int(choice)
  18.         return choice
  19.  
  20.     def perform(self, choice):       # based on choice, call the appropriate handler function
  21.         if (choice == 1):
  22.             num1 = int(input('Enter the first number :'))
  23.             num2 = int(input('Enter the second number :'))
  24.            
  25.             sum = num1 + num2
  26.             print('\nThe sum of ' + str(num1) + ' and ' + str(num2) + ' is equal to ' + str(sum))
  27.             return
  28.         elif (choice == 3):                         # average of n numbers
  29.             num = int(input('How many numbers do you want to take average of:'))
  30.             i = 1
  31.             total = 0
  32.             while i<=num:
  33.                 s = 'Enter number ' + str(i) + ' '
  34.                 temp = int(input(s))
  35.                 total += temp
  36.                 i += 1
  37.                
  38.             print('\nThe average of all the numbers you entered is ' + str(total/num))
  39.             return
  40.         elif (choice == 2):                         # add n numbers
  41.             num = int(input('How many numbers do you want to add:'))
  42.             i = 1
  43.             total = 0
  44.             while i<=num:
  45.                 s = 'Enter number ' + str(i) + ' '
  46.                 temp = int(input(s))
  47.                 total += temp
  48.                 i += 1
  49.                
  50.             print('\nThe sum of all the numbers you entered is ' + str(total))
  51.             return
  52.         elif (choice == 4):                         # need to print the menu
  53.             # do nothing, the menu will print again automatically due to the loop in main function
  54.             return
  55.         elif (choice == 5):                         # exit the program
  56.             self.endProgram()
  57.         else:                                       # wrong input
  58.             print("\nYou have not entered a valid choice. Please try again.")
  59.  
  60.     def endProgram(self):                   # function to exit
  61.         x = raw_input("\nAre you sure you want to Exit? (y/n) : ")        # ask for confirmation
  62.         if (x == 'y'):
  63.             print("\nExiting... Bye\n\n")            
  64.             exit()
  65.         else:
  66.             return
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement