Contradicting statements
answer = 7 # sets the answer
answer = guess
Here, you are setting a variable only to immediately set it to another value in the very next line, effectively invalidating the first line. This also makes your code confusing: you do that thing you are doing in these lines, and then you start comparing the guess with this magic number 7 (which is was answer
used to be).
Also, why is the answer being set to the guess? Those are two completely different things. Instead of what you are doing, to make your code much more understandable, you should get rid of this line:
answer = guess
Then, instead of comparing guess
with 7
, compare it with answer
.
if guess < answer:
print "That's too low."
if guess > answer:
print "That's too high."
if guess == answer:
print "Good. Thanks for playing."
Now your code is much more readable and follows better practices.
elif
Using elif
is good because if the first conditional check fails, the rest will not be executed, which speeds up your code. Unfortunately, elif
can not be used in every case due to specific code-based reasons.
Luckily for you, your code can use elif
. So, instead of having three separate if
statements, you should have one with two elif
s:
if guess == answer:
print "Good. Thanks for playing."
elif guess < answer:
print "That's too low."
else:
print "That's too high."
I also re-arranged your code. Now, the most unique conditional comes first. And, instead of a final elif
, I put an else
because if the guess is not greater than or equal to the answer, it has to be less than it.