Welcome to the Treehouse Community
Looking to learn something new?
Why does this code not work ?
I've noticed that after removing the diff1 and diff2 variables from the entire code , it starts to work just fine but why does it not work when they are there ?
Jason AndersTreehouse Moderator 145,624 Points
Hi bavan .
You code is looking great, and the errors you are experiencing is because of how you have the conditionals set up and where you are assigning those 2 variables that are giving you the problem.
Right now, you have the assignments happening inside of the
if block of your conditionals which is checking if the
guess does EQUAL the
random number. If it does, great the person won. However, if it doesn't, the interpreter does not even see the code inside that block, so it is not seeing the assignments for those two variables.
When the interpreter gets to the
else if and tries to use the variable
diff2 it can't find it, because it was never assigned.
To solve this, you will need to move the
diff2 assignment to inside the first
else if and the
diff1 to inside the second
else if. Once done, the code does run perfectly.
Hope this helps.
Nice work!! :)