Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import time
- from termcolor import colored
- def main():
- if len(sys.argv) < 4:
- print(colored("Penggunaan: python kalkulator.py [operasi] [operand1] [operand2]", "red"))
- return
- operasi = sys.argv[1]
- operand1 = float(sys.argv[2])
- operand2 = float(sys.argv[3])
- print("Melakukan perhitungan...")
- time.sleep(1)
- if operasi == "tambah":
- hasil = operand1 + operand2
- print(colored("Hasil penjumlahan: " + str(hasil), "green"))
- elif operasi == "kurang":
- hasil = operand1 - operand2
- print(colored("Hasil pengurangan: " + str(hasil), "green"))
- elif operasi == "kali":
- hasil = operand1 * operand2
- print(colored("Hasil perkalian: " + str(hasil), "green"))
- elif operasi == "bagi":
- if operand2 == 0:
- print(colored("Error: Pembagian oleh nol tidak diperbolehkan.", "red"))
- else:
- hasil = operand1 / operand2
- print(colored("Hasil pembagian: " + str(hasil), "green"))
- else:
- print(colored("Operasi tidak valid. Operasi yang tersedia: tambah, kurang, kali, bagi", "red"))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement