# Kalkulator OOP

May 12th, 2021
544
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import os
2. from colored import fg, bg, attr
3.
4. def clear():
5.   os.system("clear")
6.
8.   print(f"{attr(1)}    Program Operasi Bilangan dengan Python 3{attr(0)} \n")
9.
10. class Kalkulator:
11.   def __init__(self,bil1,bil2):
12.     self.bil1 = bil1
13.     self.op = op
14.     self.bil2 = bil2
15.
16.   def tambah(self):
17.     print(f"    Hasil : {fg(4)}{self.bil1}{attr(0)} + {fg(4)}{self.bil2}{attr(0)} = {fg(2)}{attr(1)}{self.bil1 + self.bil2}{attr(0)}")
18.
19.   def kurang(self):
20.     print(f"    Hasil : {fg(4)}{self.bil1}{attr(0)} - {fg(4)}{self.bil2}{attr(0)} = {fg(2)}{attr(1)}{self.bil1 - self.bil2}{attr(0)}")
21.
22.   def kali(self):
23.     print(f"    Hasil : {fg(4)}{self.bil1}{attr(0)} * {fg(4)}{self.bil2}{attr(0)} = {fg(2)}{attr(1)}{self.bil1 * self.bil2}{attr(0)}")
24.
25.   def bagi(self):
26.     print(f"    Hasil : {fg(4)}{self.bil1}{attr(0)} / {fg(4)}{self.bil2}{attr(0)} = {fg(2)}{attr(1)}{self.bil1 / self.bil2:.2f}{attr(0)}")
27.
28.   def mod(self):
29.     print(f"    Hasil : {fg(4)}{self.bil1}{attr(0)} % {fg(4)}{self.bil2}{attr(0)} = {fg(2)}{attr(1)}{self.bil1 % self.bil2:.2f}{attr(0)}")
30.
31. while(True):
33.   try:
34.     bil1 = int(input(f"    Masukkan bilangan 1        : {fg(4)}{attr(1)}"))
35.   except:
36.     clear()
37.     print(f"    {attr(0)}{bg(1)}Input tidak valid! Masukkan nilai integer{attr(0)}")
38.     continue
39.
40.   op = input(f"\n{attr(0)}    Pilih operator (+,-,*,/,%) : {fg(4)}{attr(1)}")
41.   if not op in ('+','-','*','/','%'):
42.     clear()
43.     print(f"    {attr(0)}{bg(1)}Input tidak valid! Pilih operator yang benar{attr(0)}")
44.     continue
45.
46.   try:
47.     bil2 = int(input(f"\n{attr(0)}    Masukkan bilangan 2        : {fg(4)}{attr(1)}"))
48.     print(attr(0))
49.     break
50.   except:
51.     clear()
52.     print(f"    {attr(0)}{bg(1)}Input tidak valid! Masukkan nilai integer{attr(0)}")
53.     continue
54.
55. calc = Kalkulator(bil1,bil2)
56.
57. clear()