Python - How do I fix this speed varible writing back to file? : I've been writing a program, I've run into an error. My current code is: 

    import tkinter as tk
    speed = 80
    def onKeyPress(event, value):
        global speed 
        text.delete("%s-1c" % 'insert', 'insert')
        text.insert('end', 'Current Speed: %s\n\n' % (speed, ))
        with open("speed.txt", "r+") as p:
            speed = p.read()
            speed = int(speed)
        speed = min(max(speed+value, 0), 100)
        with open("speed.txt", "r+") as p:
            p.writelines(str(speed))
        print(speed)
        if speed == 100:
            text.insert('end', 'You have reached the speed limit')
        if speed == 0:
            text.insert('end', 'You can not go any slower')
    
    
    
    speed = 80
    
    root = tk.Tk()
    root.geometry('300x200')
    text = tk.Text(root, background='black', foreground='white', font=('Comic Sans MS', 12))
    text.pack()
    
    # Individual key bindings
    root.bind('<KeyPress-w>', lambda e: onKeyPress(e, 1)) 
    root.bind('<KeyPress-s>', lambda e: onKeyPress(e, -1)) #
    
    root.mainloop()

I believe speed = min.... is causing the error? However do you guys have any idea?