I'm new at BTC mining and I'm still learning. I'm almost medium level in Python.

I'm looking for a Python miner that automatically mines using the last block and stops when a new block is found and if I'm lucky, the algorithm adds the new block to the blockchain and sends the prize to a specific wallet address that I can add.

But now for days I'm still looking and nothing.

I find a simple one, it keeps looking, but even if it finds a new block I don't know how to add that to the blockchain, and I don't know if the last block has been changed or not.

Is there any Python algorithm that do all of that, or a few lines of Python code which I can change?

  • 1
    Does this answer your question? In the ASIC-age, is it worth starting mining Bitcoin at home? Nov 17, 2021 at 22:02
  • thank you for the answer it contains a lot of useful information, but no it did not answer my question I'm looking for a python code that I can modify and it run automatically (get the last block, mine, if I'm lucky it add the block to the block chain, and send price to a specific wallet address)
    – houhou
    Nov 17, 2021 at 22:17
  • I think the point that RedGrittyBrick was making is that you will never find a block mining with a regular computer. Today, you need ASICs to stand a chance to find a block in a conceivable time.
    – Murch
    Nov 18, 2021 at 21:24
  • thank you very much, yes I red the ASIC's article and am looking to learn how to do it with python just for fun if I want to really mine, I will mine another coin, just to learn how to do it and how to make it automatically work, if there is any python ago, or a few hint or an old article because I'm trying to find it for day's now and I find nothing, and thank's again for your help
    – houhou
    Nov 19, 2021 at 18:50

1 Answer 1


here is an easy hashminer

from hashlib import sha256
MAX_NONCE = 100000000000

def SHA256(text):
    return sha256(text.encode("ascii")).hexdigest()

def mine(block_number, transactions, previous_hash, prefix_zeros):
    prefix_str = '0'*prefix_zeros
    for nonce in range(MAX_NONCE):
        text = str(block_number) + transactions + previous_hash + str(nonce)
        new_hash = SHA256(text)
        if new_hash.startswith(prefix_str):
            print(f"Yay! Successfully mined bitcoins with nonce value:{nonce}")
            return new_hash

    raise BaseException(f"Couldn't find correct has after trying {MAX_NONCE} times")

if __name__=='__main__':
    difficulty=4 # try changing this to higher number and you will see it will take more time for mining as difficulty increases
    import time
    start = time.time()
    print("start mining")
    new_hash = mine(5,transactions,'0000000xa036944e29568d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7', difficulty)
    total_time = str((time.time() - start))
    print(f"end mining. Mining took: {total_time} seconds")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.