0

I would like to know how to write a function that is repeated without getting the error message :

RecursionError: maximum recursion depth exceeded in instancecheck_

The repeated recursive function is main():

import tkinter as tkin
import math
import hmac
import hashlib
import sys

n = 0
sys.setrecursionlimit(2450)
root= tkin.Tk()
canvas1 = tkin.Canvas(root, width = 300, height = 300)
canvas1.pack()
label1 = tkin.Label(root, text= 'Number', fg='green', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 200, window=label1)

def dice ( serverSeed, clientSeed, nonce ):
    round = 0
    nonceSeed = '{}:{}:{}'.format(clientSeed, nonce, round)
    hex = hmac.new(bytes(serverSeed, 'utf-8'), bytes(nonceSeed, 'utf-8'), hashlib.sha256).hexdigest()[0:8]
    i = 0
    end = 0
    while i < 4:
        end += int(hex[i*2:i*2+2], 16) / math.pow(256, i+1)
        i+=1
    end = math.floor(end * 10001) / 100
    return str(end)
    
def main ():
    global n
    n += 1
    roll = float(dice('535e8f53eee1402b242c7eff4038787d3de850c3ba27bde6a370225e1a2f23dd', '8cf82c02b3', n))
if n % 2400 == 0:
        label1.configure(text=roll)
        label1.update();
    main()
   
button1 = tkin.Button(text='Click Me',command=main, bg='brown',fg='white')
canvas1.create_window(150, 150, window=button1)

root.mainloop()

Edit: Added limit for recursion and root.after(int, function)

limit = 2400
def main ():
    global n
    global max
    n += 1
    roll = float(dice('535e8f53eee1402b242c7eff4038787d3de850c3ba27bde6a370225e1a2f23dd', '8cf82c02b3', n))
    if n % limit == 0:
        label1.configure(text=roll)
        label1.update();
        limit+=2400
        root.after(0, main)
    else:
        main()

2 Answers2

0

Why don't you write something like this

def main ():
    n = 0
    while True:
        n += 1
        roll = dice('535e8f53eee1402b242c7eff4038787d3de850c3ba27bde6a370225e1a2f23dd', '8cf82c02b3', n)
        if n % 2440 == 0:
            label1.configure(text=roll)
            label1.update();

edit - I see people have suggested this while I typed the answer

0

Why do you want to go past the maximum recursion? This is not what recursion is for.
While this will not answer your question, as I see no point in increasing recursion depth, I will try to explain a few things.

Recursion is not a substitute for a loop. Yes, it can replace a loop, but it works differently.

Every time you call a function, the data in the function is saved in your memory until that function returns or until that memory is released in a different way (uncaught exception, del or overriding).

Imagine a theoretical function that loads 500MB to the memory, if you call it within itself, you will have 1GB in your memory. Call it again in itself and you will have 1.5GB.
Imagine what happens if you decide to do an infinite recursion. If this recursion reaches a return condition then all that memory will be released, otherwise it will keep on stacking until you run out of memory.

Work with loops, they are better for what you need.

BoobyTrap
  • 967
  • 7
  • 18