Thursday, April 27, 2023

 Simple Python Calculator


This script will allows your to calculate the integers given with the chosen operation. You can add, substract, multiply and divide the numbers.

class kalkulator:
    def __init__(self,no1,no2):
        self.no1 = int(no1)
        self.no2 = int(no2)
    #formula
    def tambah(self):
        return self.no1 + self.no2
    def tolak(self):
        return self.no1 - self.no2
    def darab(self):
        return self.no1 * self.no2
    def bahagi(self):
        if self.no2 <= 0:
            print("Nombor kurang atau sama dengan sifar. Tidak boleh dibahagi")
        else:
            return self.no1 / self.no2

def main():
    print("Kalkulator\nTekan q untuk keluar")
    chkNo1 = input("Masukkan nombor pertama : ")

    #semak sama ada integer atau q untuk exit program
    if chkNo1 == "q":
        print("Tamat!","\n")
        exit
    else:
        chkNo2 = input("Masukkan nombor kedua : ")
        if bool(int(chkNo1)) == False:
            print("Masukkan integer sahaja")
            main()
        elif bool(int(chkNo2)) == False:
            print("Masukkan integer sahaja")
            main()
        else:
            no1 = chkNo1
            no2 = chkNo2
            kira = kalkulator(no1,no2)

            operasi = int(input("Pilih operasi : tambah(1), tolak(2), darab(3), bahagi(4) : "))
            if operasi == 1:
                print("Jawapannya adalah : ", kira.tambah())
            elif operasi == 2:
                print("Jawapannya adalah : ", kira.tolak())
            elif operasi == 3:
                print("Jawapannya adalah : ", kira.darab())
            elif operasi == 4:
                print("Jawapannya adalah : ", kira.bahagi())
            else:
                print("\n","RALAT: Sila masukkan pilihan operasi yang betul")
            print("=="*14,"\n")
               
        main() #akan loop pada main selagi tidak stop script

main() #default akan run main

Wednesday, April 26, 2023

 Getting ip address using python

Script to get the ip address from domain name. Here's the code

import socket

# fungsi untuk semak dns
def check_dns(hostname):
    try:
        # menyemak dns
        ip = socket.gethostbyname(hostname)
        print(hostname, " IP ditemui:", ip)
        print("Address Info: ", trace)
    except socket.gaierror:
        # jika dns tidak ditemui
        print("DNS tidak ditemui")

# menjalankan fungsi
check_dns("www.google.com")
check_dns("www.example.com")
check_dns("www.mynic.my")

Simple OpenAi Python Script

 Make your own OpenAi chat


I am totally new comer in Python trying to learn this language from scratch. Knowing a basic of python language is a quite easy for me as it is not a big different from JavaScript (if not mistaken). As the OpenAi are become popular today i take a chance to learn on how to make a simple OpenAi script using Python. 

I usually use Microsoft Visual Studio Code to scripting. We have to install OpenAi extension first by typing this command to the terminal:

pip install openai

then after that, create a new python file named ai.py and put in this codes. Save it and run it using debugger or type python3 ai.py:

Hope this helps

import openai

openai.api_key='get-your-own-key' #generate it from openai.com

while True:
    model_engine ="text-davinci-003" #model 3 openai
    prompt = input("[AI] Apa yang anda ingin tahu? : ")
    if prompt == "exit" or prompt == "quit":
        break

    completion = openai.Completion.create(
        engine = model_engine,
        prompt = prompt,
        max_tokens = 4000, #jumlah maksimum teks,
        n = 1, #tetap hanya sekali respon,
        stop = None,
        temperature = 0 #semakin tinggi akan memberi kesan tidak accurate
    )
    response = completion.choices[0].text

    print(response + "\n")
    print("=="*40)
    print("\n")


 Simple Python Calculator This script will allows your to calculate the integers given with the chosen operation. You can add, substract, mu...