It depends on the genre, obviously, but if we're talking about anything with a cerebral element of planning, customization, strategy, or decision-making, the answer is like this: hard enough that decisions and personal resourcefulness still matter, and multiple approaches or solutions exist; not so hard that you're funnelled into a narrow band of optimality. Usually, this is on the harder end of the provided scale, but not cranked all the way to the top.
Essentially, the perfect difficulty is one that makes me feel like I'm scraping by thanks to my agency as a player: something I'm doing or teaching myself to do. Too easy, and your decisions don't matter. Punishing to the point that you can only do things one way, or that success and failure are at the mercy of RNG, and you don't even have decisions.
This carries over somewhat into action games or platformers, where your decisions (mechanical, perceptual, exploratory) are made in the spur of the moment. But again, it depends on the genre. I've dialled down shooters to Easy if I just didn't find their kind of challenge fun to test myself against repeatedly. And not all kinds of difficulty are the same. I prefer to be rewarded for mastering something consistently than for narrowly clearing the bar by accident once.
Essentially, the perfect difficulty is one that makes me feel like I'm scraping by thanks to my agency as a player: something I'm doing or teaching myself to do. Too easy, and your decisions don't matter. Punishing to the point that you can only do things one way, or that success and failure are at the mercy of RNG, and you don't even have decisions.
This carries over somewhat into action games or platformers, where your decisions (mechanical, perceptual, exploratory) are made in the spur of the moment. But again, it depends on the genre. I've dialled down shooters to Easy if I just didn't find their kind of challenge fun to test myself against repeatedly. And not all kinds of difficulty are the same. I prefer to be rewarded for mastering something consistently than for narrowly clearing the bar by accident once.