Advertisement
SamGauths

Calculator in Python

Dec 13th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. import os
  2.  
  3.  
  4.  
  5. def showMenu(): # Function that display the menu
  6.     print('1. Addition')
  7.     print('2. Subtraction')
  8.     print('3. Multiplication')
  9.     print('4. Division')
  10.     print('5. Exit\n')
  11.  
  12.  
  13. def myChoice(): # Take your choice in input
  14.     c = input('')
  15.     return int(c)
  16.  
  17.  
  18. def select(c):  # This function prints the choice you entered
  19.     if c == 1:
  20.         print('\nAddition')
  21.     elif c == 2:
  22.         print('\nSubtraction')
  23.     elif c == 3:
  24.         print('\nMultiplication')
  25.     elif c == 4:
  26.         print('\nDivision')
  27.     else:
  28.         exit(1) # Forces the exit if we enter the value 5
  29.  
  30.  
  31. def calc(a, b, c):  # Returns the result of the caclculations
  32.     if c == 1:
  33.         return a+b
  34.     elif c == 2:
  35.         return a-b
  36.     elif c == 3:
  37.         return a*b
  38.     elif c == 4:
  39.         return a/b
  40.  
  41.  
  42. def signFunc(c):  # Change the sign depending on your choice
  43.     if c == 1:
  44.         return' + '
  45.     elif c == 2:
  46.         return ' - '
  47.     elif c == 3:
  48.         return ' * '
  49.     elif c == 4:
  50.         return ' / '
  51.  
  52.  
  53. mainLoop = 1;
  54.  
  55. while mainLoop : # Infinite loop
  56.     sign = ' + ';
  57.     showMenu()
  58.     c = myChoice()
  59.     select(c)
  60.     sign = signFunc(c)
  61.  
  62.     print('Enter the first number: ')
  63.     entry1 = input('');  # First entry
  64.     print('Enter the second number: ')
  65.     entry2 = input('');  # Second entry
  66.  
  67.     entry1 = float(entry1) # Convert our entries into floats
  68.     entry2 = float(entry2)
  69.  
  70.     result = calc(entry1, entry2, c)
  71.  
  72.     print('\n' + str(entry1) + sign + str(entry2) + ' = ' + str(result) + '\n\n')
  73.  
  74. os.system('Pause')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement