Advertisement
Guest User

main.py

a guest
Dec 18th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.70 KB | None | 0 0
  1. #-*-coding:utf8;-*-
  2. #qpy:3
  3. #qpy:console
  4. import math
  5. print("""Calculator programmed my ahmed
  6.     **
  7.         **
  8.             **
  9. the Calculator can perform the following functions
  10. 1 four basic operations (add,multiply, subtract and divide)
  11. 2.Squares and Square root
  12. 3.mean
  13. 4.range
  14. 5.area and volume of basic shapes
  15. 6.factorials
  16. 7.trigonmetric operations (cos,sin and tan
  17. 8.logarithmic functions
  18. 9.modular arithmetic
  19. 10.One line maths(+ - / *)
  20. 10.many mor features to be added soon""")
  21.  
  22. def calculate ():
  23.  print("Enter 'add' to add two numbers")
  24.  print("Enter 'subtract' to subtract two numbers")
  25.  print("enter 'oneline' to add subtract or divide more than two numbers")
  26.  print("Enter 'multiply' to multiply two numbers")
  27.  print("Enter 'divide' to divide two numbers")
  28.  print("Enter 'others' For more functions")
  29.  print("Enter 'mod' for modular arithmetic")
  30.  print("Enter 'quit' to end the program")
  31.  
  32.  user_input = input(": ")
  33.  
  34.  if user_input == "add":
  35.   num1 = float(input("Enter a number: "))
  36.   num2 = float(input("Enter another number: "))
  37.   result = str(num1 + num2)
  38.   print(result)
  39.  elif user_input == "subtract":
  40.   num1 = float(input("Enter a number: "))
  41.   num2 = float(input("Enter another number: "))
  42.   result = str(num1 - num2)
  43.   print(result)
  44.  elif user_input == "oneline":
  45.      def oneline():
  46.          try:
  47.           oneline_math = eval(input("enter math expression (e.g 1+1+1, 5*4*1) "))
  48.           print(oneline_math)
  49.           print("do you wish to repeat for yes enter 'y' for no enter 'n' ")
  50.           def repeat():
  51.             repl = input("y or n:" )
  52.             if repl == "y":
  53.              oneline()
  54.             elif repl =="n" :
  55.              calculate()
  56.             else:
  57.              print("try again")
  58.              repeat()
  59.           repeat()  
  60.          except SyntaxError:
  61.           print("syntax error please try again")
  62.           oneline()
  63.          except ZeroDivisionError:
  64.           print("don't divide by zero try again")
  65.           oneline()
  66.          except :
  67.           print ("error try again")
  68.           oneline()
  69.      oneline()
  70.  elif user_input == "multiply":
  71.   def multiply():
  72.    try:
  73.     num1 = float(input("Enter a number: "))
  74.     num2 = float(input("Enter another number: "))
  75.     result = str(num1 * num2)
  76.     print(result)
  77.    except:
  78.     print("error try again")
  79.     multiply()
  80.   def repeat():
  81.             print("do you wish to repeat for yes enter 'y' for no enter 'n' ")
  82.             repl = input("y or n:" )
  83.             if repl == "y":
  84.               multiply()
  85.             elif repl =="n" :
  86.              calculate()
  87.             else:
  88.              print("try again")
  89.              repeat()
  90.   multiply()            
  91.   repeat()  
  92.  elif user_input == "divide":
  93.   num1 = float(input("Enter a number: "))
  94.   num2 = float(input("Enter another number: "))
  95.   result = str(num1 / num2)
  96.   print(result)
  97.  elif user_input == "mod":
  98.   num1 = float(input("Enter a number: "))
  99.   num2 = float(input("Enter another number: "))
  100.   result = str(num1 % num2)
  101.   print(result)
  102.  elif user_input == "quit":
  103.   exit()
  104.  elif user_input == "others":
  105.   def others():
  106.    print("waiting")
  107.  
  108.    print ("""
  109. for Sine  type sin
  110. for Cosine of type cos
  111. for Tangeant type: tan
  112. for Square root type: sqrt
  113. for square type sq
  114. for log type log
  115. for factorials type factorial
  116. for mean or range type statistics
  117. for area of a (rectangle/square, triangle, circle) type area
  118. type back to go back
  119. type q to quit
  120. """)
  121.  
  122.    fcn = input(": ")
  123.    print(fcn)
  124.    if fcn == "sin":
  125.       num=float(input("enter number: "))
  126.       result = float(math.radians(num))
  127.       print(str(math.sin(result)))
  128.    elif fcn == "cos":
  129.       num=float(input("enter number: "))
  130.       result = float(math.radians(num))
  131.       print(str(math.cos(result)))
  132.    elif fcn == "tan":
  133.       num=float(input("enter number: "))
  134.       result = float(math.radians(num))
  135.       print(str(math.tan(result)))
  136.    elif fcn == "sqr":
  137.       num=float(input("enter number: "))
  138.       result = float(num) * float(num)
  139.       another  ="square of " +  (str(num)) + " is " + (str(result))
  140.       print(another)
  141.    elif fcn == "back":
  142.          calculate()
  143.    elif fcn == "sqrt":
  144.           num=float(input("enter number: "))
  145.           result = float(math.sqrt(num))
  146.           another  ="square root of " +  (str(num)) + " is " + (str(result))
  147.           print(another)
  148.    elif fcn == "q":
  149.     print("bye bye")
  150.     exit()
  151.    elif fcn == "factorial":
  152.        num=int(input("enter number: "))
  153.        result = int(math.factorial(num))
  154.        another = (str(num)) + "! " + "is " + (str(result))
  155.        print(another)
  156.    else:
  157.     others()
  158.   others()  
  159.  else:
  160.   calculate()
  161.  
  162. def again():
  163.  while 1==1:
  164.   print("any other calculations ?? enter yes or no")
  165.   answer = input(": ")
  166.   if answer == "yes":
  167.      calculate()
  168.   elif answer == "no":
  169.    print("bye bye")
  170.    exit()
  171.   else:
  172.    again()
  173.  
  174. calculate()
  175. again()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement