There is a psalm which reads:
18 If I had harbored sin in my heart,
the sovereign Master would not have listened.
If I regard wickedness in my heart,
The Lord will not hear (NASB).
There are many reasons why God does not answer our prayers the way we would like. One reason is that we are willfully persisting in sin and disobedience. In such cases, the verse above would apply.
There are times when we are simply asking for the wrong things, or for the wrong reasons:
3 you ask and do not receive because you ask wrongly, so you can spend it on your passions. The NET Bible
Sometimes we ask sincerely and in faith, but it is simply not God’s will:
8 I asked the Lord three times about this, that it would depart from me. 9 But he said to me, “My grace is enough for you, for my power is made perfect in weakness.” So then, I will boast most gladly about my weaknesses, so that the power of Christ may reside in me. 10 Therefore I am content with weaknesses, with insults, with troubles, with persecutions and difficulties for the sake of Christ, for whenever I am weak, then I am strong. The NET Bible
If God were only to answer the prayers of those who were perfectly clean, and without any unconfessed sin, there would be no prayers answered. Paul indicates in 1 Corinthians 4:1-5 that we are not capable of judging ourselves, and that this will only be done by our Lord, when He returns.
If we persist in willfully practicing sins which we know to be wrong, then we certainly have no right to expect our prayers to be answered. If the answers to our prayers were dependent on our worthiness, then we would never have any prayer answered. Prayer in our way of communicating to God that we are sinful, needy, people, in desperate need of divine intervention. Prayer should never be based on our righteousness, but on His mercy and grace.
Compare the two prayers of Luke 18:9-14.