Advertisement
furas

Python - Calc (FB - LearnPython.org)

Aug 23rd, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. #
  4. # https://www.facebook.com/groups/learnpython.org/permalink/1173109739420308/
  5. #
  6.  
  7. # its a claculater program
  8.  
  9. # in the begning we are creating our own function using defr operator
  10. def menu():
  11.     # print what we want to print
  12.     print("welcome to claculator")
  13.     print("your options are:")
  14.     print()
  15.     print("1. addition")
  16.     print("2. subtraction")
  17.     print("3. multiplication")
  18.     print("4. division")
  19.     print("5. quit clac")
  20.     print()
  21.     return input("choose your option: ")
  22.  
  23. # now def fn for add two nos and same for all
  24. def add(a, b):
  25.     print(a, "+", b, "=", a+b)
  26.  
  27. def sub(a, b):
  28.     print(a, "-", b, "=", a-b)
  29.  
  30. def mul(a, b):
  31.     print(a, "*", b, "=", a*b)
  32.  
  33. def div(a, b):
  34.     print(a, "/", b, "=", a/b)
  35.  
  36. # now code really starts from here after defining
  37.  
  38. while True:
  39.  
  40.     choice = menu()
  41.  
  42.     # choice is a text so compare with text "1", "2", etc.
  43.     if choice == "1":
  44.         x = int(input("add this: "))
  45.         y = int(input("to this : "))
  46.         add(x, y)
  47.     elif choice == "2":
  48.         x = int(input("subtract this: "))
  49.         y = int(input("from this : "))
  50.         sub(x, y)
  51.     elif choice == "3":
  52.         x = int(input("multiply this: "))
  53.         y = int(input("by this : "))
  54.         mul(x, y)
  55.     elif choice == "4":
  56.         x = int(input("divide this: "))
  57.         y = int(input("by this : "))
  58.         div(x, y)
  59.     elif choice == "5":
  60.         break # leaving `while True` loop
  61.  
  62. print("thank you for using pyclac!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement