Advertisement
paperline27

kalkulator.py

Feb 26th, 2024
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | Source Code | 0 0
  1. import sys
  2. import time
  3. from termcolor import colored
  4.  
  5. def main():
  6.     if len(sys.argv) < 4:
  7.         print(colored("Penggunaan: python kalkulator.py [operasi] [operand1] [operand2]", "red"))
  8.         return
  9.    
  10.     operasi = sys.argv[1]
  11.     operand1 = float(sys.argv[2])
  12.     operand2 = float(sys.argv[3])
  13.  
  14.     print("Melakukan perhitungan...")
  15.     time.sleep(1)
  16.  
  17.     if operasi == "tambah":
  18.         hasil = operand1 + operand2
  19.         print(colored("Hasil penjumlahan: " + str(hasil), "green"))
  20.     elif operasi == "kurang":
  21.         hasil = operand1 - operand2
  22.         print(colored("Hasil pengurangan: " + str(hasil), "green"))
  23.     elif operasi == "kali":
  24.         hasil = operand1 * operand2
  25.         print(colored("Hasil perkalian: " + str(hasil), "green"))
  26.     elif operasi == "bagi":
  27.         if operand2 == 0:
  28.             print(colored("Error: Pembagian oleh nol tidak diperbolehkan.", "red"))
  29.         else:
  30.             hasil = operand1 / operand2
  31.             print(colored("Hasil pembagian: " + str(hasil), "green"))
  32.     else:
  33.         print(colored("Operasi tidak valid. Operasi yang tersedia: tambah, kurang, kali, bagi", "red"))
  34.  
  35. if __name__ == "__main__":
  36.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement