There is a saying by the sages that goes as follows: "God forges the good thought into a good deed. He does not forge a bad thought into a deed." In other words, if a person thinks of doing a good deed, God gives them credit as if they had done it. A thought of sin, however, does not accrue to the thinker's detriment.
An example, Reuven gets credit for having saved Joseph's life by suggesting that the brothers throw him in a pit rather than kill him. The sages, however, claim that the pit had snakes and scorpions in it! That's not a recommended way to save someone's life! But Reuven didn't see those lethal creatures because the pit was too deep. He thought he was saving Joseph's life, and the Bible gives him credit for having done so.
Why is this the case? How does this process of transforming a good thought into a deed actually work?
The Bible calls for animal sacrifices. In modern times, animal sacrifices sounds a bit primitive. There is a big dispute among the sages regarding the reason for animal sacrifices. According to Maimonidies, the great medieval scholar, sacrifices are a response to what was happening in the pagan world. The pagans practiced all kinds of sacrifice, and so God gave the Jewish people their own version of it.
Implied in this answer is the understanding that when idolatry ceases to be a factor in the world, the need for animal sacrifices disappears. Nonetheless, biblical and rabbinical sources imply that the sacrificial order will be reinstated at the time of the third Temple's construction. This is a question raised against Maimonidies' opinion.
Another medieval scholar, Rabbi Moses Nachmanidies, gives another reason. When a person brings a sacrifice, they see the animal slaughtered and burned on the altar. They think of how their sins really made them worthy of that punishment, but God had mercy on them. The animal sacrifice becomes a catalyst for repentance.
Even though they disagree, I believe there is a common thread in both of their answers. They both imply that the sacrifices are a response to a situation, either in the religious world, or in the heart of the one bringing the sacrifice. Maimonidies is looking at the psychology of the Jewish people, and he is saying that it is unhealthy for the Jews to feel that the pagans are more "religious" than them. If the Jews felt that the pagans are bringing sacrifices and the Jews themselves are not, a certain sense of religious inferiority might be felt. Once that practice became established, however, it remains for all generations. The same reason applies, so the future generations shouldn't feel that they aren't as religious as their predecessors.
I would like to delve deeper in this discussion, because there is gold to be found in it. The first sacrifice brought was by Cain and his brother Abel. There was no idolatry at that time either, yet they brought sacrifices. Each one had his personal reason for doing so. Cain was seeking to appease God, who was angry about the sin of Adam and Eve. His motivation was guilt. Guilt is a weaker motivation, and Cain sought to bring the "cheapest" sacrifice he could. He brought some of his fruits, not the best, not the worst, just average. God did not accept it.
Abel had a different motivation: love of God. He was inspired by God's creation, grateful for his own humanity, and sought to express that through a gift, a sacrifice. The lover always seeks to bring the best, and Abel brought the firstborn of his flocks. This was accepted.
Thus, there are two motivations for bringing sacrifices, or doing any good deed. The best is when it comes from the heart, and the love in the heart must overflow into those good deeds. The second-best is when it comes from the mind, even though the heart may not feel as strong a motivation. It is still better to do a good deed for neutral reasons, and I stress neutral, rather than to not do it at all. As Judaism teaches, "It is better to perform the commandments not for their own sake, because by doing so, they will come to be performed for their own sake." In other words, doing good deeds transforms the heart.
I believe that both Maimonidies and Nachmanidies are referring to the latter motivation. They are talking about people who may not feel tremendous religious motivation, but know that they should be doing good deeds. An external cause for bringing a sacrifice, whether it be societal pressure or a sense of guilt. In an ideal world, however, sacrifices are brought out of love of God. That applies in a time when there is no idolatry, and when people are not as sinful as before. These scholars are talking about the majority of the history of the world, when the heart may not be as pure and when external motivations become necessary.
There are verses in the prophets, however, that seem to contradict this idea of doing the good deed, bringing the sacrifice, even if not motivated by love. "Why do I need all of your sacrifices when your hearts are far from Me?" Why? We just gave a reason! Because doing the good deed changes the heart!
It is because people can also do good deeds for bad reasons. Up until now, we have been talking about neutral reasons. The person goes to synagogue because it is the thing to do, not because they love the synagogue. But when a person gives charity from stolen money, that charity has no value. The charity becomes part of the sin, because the person seeks to justify his evil deed by giving charity. This is called whitewashing. Someone who wants to bribe God with a sacrifice so He will look the other way when they cheat and steel, will get no credit for that sacrifice.
Thus, the meaning of the statement we quoted above, "God forges the good thought into a good deed. He does not forge a bad thought into a deed," takes on a new meaning. This saying can be understood as giving us a process. It can mean that a good thought leads to a valid deed, and combines with it to strengthen the good heart. A bad thought, however, does not lead to a valid deed, since even the deed is a sin.
So we see that there is value in doing good deeds for external reasons, as long as those reasons are positive and good. We don't need perfection, we don't need complete purity, in order to gain credit for good deeds. Which brings us to Purim.
Purim, at its essence, is compromised happiness at best. Remember, the same king Ahashverosh that had acquiesced in Haman's plan to annihilate the Jewish people was still on the throne the day after the Jews were saved. He could easily revive the decree at any time. So how can we be happy? Our hearts don't really feel it!
Therefore, comes the same solution as with the sacrifices. Perform actions of happiness with good intentions, and your heart will follow. Dress up in costumes, make merry, have a Lechaim! Do things to demonstrate happiness and gratitude to God, and even though the future is uncertain, you will feel that happiness growing inside your heart.