So, I will start by saying "Yes, you can do it. It's not too late and programming is fun and fulfilling".
However! One thing my experience has taught me in seeing people approach and bounce off programming is: programming is a fail-til-you-get-it type of endeavour. Your first several years will be littered with broken code, because there are a thousand little things you have to bump up against before you unlock one more puzzle piece.
So! If you go for it, persevere! You aren't a bad programmer, or a slow learner, because you can't get your code to work. Every single one of us ran into the same issue, and we just had to push through, learn to Google, and try again until it sorta-kinda works. You in 10 years will be embarrassed by what you write in your first years