[Tutor] Debugging While Loops for Control

Alan Gauld alan.gauld at btinternet.com
Thu Feb 16 10:05:39 CET 2012


On 16/02/12 04:57, Luke Thomas Mergner wrote:

> My problem is that I am using two functions that return True or False
 > to determine whether the player receives another card.

> Because of the way it evaluates the while condition, it either
 > prints too little information or previously called the hitMe()
 > function too many times.

> I am assuming that I am misusing the while loop in this case.

> while ask_for_raw_input() AND is_the_score_over_21():
> 	hitMe(hand)

I haven't looked at the code for the functions but going
by their names I'd suggest you need to reverse their order to

while is_the_score_over_21() and ask_for_raw_input():
  	hitMe(hand)

The reason is that the first function will always get called
but you (I think) only want to ask for, and give out, another
card if the score is over 21 (or should that maybe be
*under* 21?).

Personally I would never combine a test function with
an input one. Its kind of the other side of the rule that
says don't don;t put print statements inside logic functions.
In both cases its about separating himan interaction/display from 
program logic. So I'd make the ask_for_raw_input jusat return a value(or 
set of values) and create a new funtion to test
the result and use that one in the while loop.

HTH,
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list