[Tutor] Query regarding loop problem

Laura Creighton lac at openend.se
Sun Aug 30 11:38:09 CEST 2015


Alan has pointed out the string/int problem with your code.
Your code has a different problem, as well.


># User enters a number between 1 - 5
># Computer generates random number until an equivalent number is achieved
>
>import random
>computerNumber = 0

Ok, now you have set computerNumber to 0

>computerNumber = input("Press enter to prompt the computer to enter a
>number: ")

As an aside -- what happens now it I type 0<return> ?  
as your code stands now, nothing, since computerNumber would
then be set to the string "0", which isn't the same as the
integer 0.  But once you get your types to match, this could
be a problem for you.

>while computerNumber != 0:

This is the line that is dodgy.   computerNumber isn't 0 to
start with (good, we will run this loop, as intended) but 
where does computerNumber get changed?

>    if myNumber != computerNumber:
>        computerNumber = random.randint(1,5)

Here.  Where it will become something in [1, 2, 3, 4, 5]
But none of these are 0s.  So your while loop will never terminate.

You continue 

>        prompt = input("Press enter to prompt the computer to enter a number: ")

You don't use this anywhere, so this is just so you can single step
through your loop, I guess.  If that is the idea, then it is better
to write it like this:  (I shortened it to fit on one line):

        prompt = input("Press enter to prompt the computer: ")
        computerNumber = random.randint(1,5)

Hit return, and get the assignment.  Not 'hit return and the next
time the loop is executed, if this happens, then sometime a
new number will be assiged'.

But back to the larger issue.

How are you going to get out of your while loop?

There are 3 different approaches you can use here.

The first way is to assign computerNumber to 0 when you have
finally found your match.  The next time around the loop, the
while condition will fail, and the loop will end.

The second way is to explicitly break out of your loop when
you have found what you are looking for with a break statement.

The second way suggests a way to improve your code.
Instead of messing around with assigning values that can never
be True,  which puts a conceptual load on the person reading your
code to trace every possible way that you could get computerNumber
to become 0 -- which, admittedly isn't a huge load in such a
tiny program.

If instead you write this as:

while True:
      if <something>:
         do stuff
      else:
         announce_victory
	 break

People who see 'while True' at the top know, for certain, that you
don't want this loop to ever terminate.  Thus you are relying on
break statements to leave, rather than 'I want to leave via the
break statement or if computerNumber becomes 0'.  Much easier to
read and understand.

The final way to get out of the loop is to replace the condition,
not with the somewhat confusing 'something that is never going
to happen normally', not with 'somethting that is very clear about
that it is never going to happen, ever' but with a test that you
are really interested in.  In this case, it is

while myNumber != computerNumber:

Hope this helps,
Laura




More information about the Tutor mailing list