[Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
Steven D'Aprano
steve at pearwood.info
Mon Apr 28 20:20:30 CEST 2014
On Mon, Apr 28, 2014 at 10:32:06AM -0700, Stephen Mik wrote:
> I must be doing something very wrong. The program is supposed to
> run a main loop ,for control of the program. The program DOES print
> out the prompts before the While Loop, but when it comes to a variable
> named"smv_guessNumber" the program DOES NOT prompt for the input for
> "smv_guessNumber" as it should. It is a mystery to me as to why the
> program will not get to the "smv_guessNumber=int(input("Think out a
> first guess:")". I am mystified why it doesn't reach that point in the
> program!
That was a tricky one! It took me a while to see it, but the problem
comes from three factors.
Firstly, these two lines:
smv_grandCounter=int(input("Enter a 1 to play or 0 to exit: "))
while(smv_grandCounter=="1"):
In the first line, you get input from the user, either "1" or "0". User
input is a string, then you convert to an int. But in the second line,
you compare it to the string "1". So regardless of whether the user
types "1" or "0", the while loop is ALWAYS skipped:
int 1 == "1" returns False
int 0 == "1" returns False
and the while loop never runs at all.
I recommend you either remove the call to int(), and keep
smv_grandCounter as a string, or you change the while condition to
while smv_grandCounter == 1:
(But don't do both!)
Secondly, since the while loop is skipped, the line which initialises
the smv_guessNumber variable:
smv_guessNumber= int(input("Take a guess!"))
also gets skipped. So smv_guessNumber doesn't get a value.
Thirdly, you haven't indented the "Number Identification Loop". The game
logic *should* be:
# Game loop
while you want to play a game:
set up the next game
# Number Identification Loop
while your guess is not equal to the number:
...
The number identification loop only happens while you want to play a
game. But you have it like this by mistake:
# Game loop
while you want to play a game:
set up the next game
# At this point, the game loop has finished,
# so you no longer wish to play
# Now you start the Number Identification Loop
while your guess is not equal to the number:
...
You need to take this line:
while(smv_guessNumber!=smv_pickNumber):
and all the code which belongs to it, and indent it one extra block, so
it is considered *inside* the "do you want to play a game?" loop.
Once you have fixed those issues, you can then continue your testing.
By the way, what is the meaning of the mysterious "smv_" prefixes on all
your variables? Please don't tell me that stands for "Stephen Mik
Variable."
It seems to me that *every* variable has the same smv_ prefix, and so
that prefix doesn't have any meaning. It would be like me deciding to
add "blah" to the beginning of every word:
blahit blahwould blahbe blahlike blahme blahdeciding blahto
blahadd "blahblah" blahto blahthe blahbeginning blahof
blahevery blahword
The "blah"s are just meaningless noise. When programming, your code
should all carry its weight. Programming is hard enough without sticking
"blah" at the beginning of every word! Variable names should describe
what the variable stands for, or at least follow some common convention
like "x" for mathematical quantities. If "smv_" doesn't carry it's
weight in helping your code be more easily understood, you should remove
it.
--
Steven
More information about the Tutor
mailing list