Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Functions:
- def __init__(self):
- pass
- def showMenu(self): # prints the menu
- print("""
- \n\t1 - Add 2 numbers\n\n
- 2 - Add n numbers\n\n
- 3 - Take average of n numbers\n\n
- 4 - Show menu again\n\n
- 5 - Exit\n
- """)
- def getChoice(self): # read user's choice
- self.showMenu()
- choice = raw_input("Your Choice : ")
- choice = int(choice)
- return choice
- def perform(self, choice): # based on choice, call the appropriate handler function
- if (choice == 1):
- num1 = int(input('Enter the first number :'))
- num2 = int(input('Enter the second number :'))
- sum = num1 + num2
- print('\nThe sum of ' + str(num1) + ' and ' + str(num2) + ' is equal to ' + str(sum))
- return
- elif (choice == 3): # average of n numbers
- num = int(input('How many numbers do you want to take average of:'))
- i = 1
- total = 0
- while i<=num:
- s = 'Enter number ' + str(i) + ' '
- temp = int(input(s))
- total += temp
- i += 1
- print('\nThe average of all the numbers you entered is ' + str(total/num))
- return
- elif (choice == 2): # add n numbers
- num = int(input('How many numbers do you want to add:'))
- i = 1
- total = 0
- while i<=num:
- s = 'Enter number ' + str(i) + ' '
- temp = int(input(s))
- total += temp
- i += 1
- print('\nThe sum of all the numbers you entered is ' + str(total))
- return
- elif (choice == 4): # need to print the menu
- # do nothing, the menu will print again automatically due to the loop in main function
- return
- elif (choice == 5): # exit the program
- self.endProgram()
- else: # wrong input
- print("\nYou have not entered a valid choice. Please try again.")
- def endProgram(self): # function to exit
- x = raw_input("\nAre you sure you want to Exit? (y/n) : ") # ask for confirmation
- if (x == 'y'):
- print("\nExiting... Bye\n\n")
- exit()
- else:
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement