[Tutor] Debugging While Loops for Control
Luke Thomas Mergner
lmergner at gmail.com
Fri Feb 17 04:27:35 CET 2012
> Message: 1
> Date: Wed, 15 Feb 2012 23:57:08 -0500
> From: Luke Thomas Mergner <lmergner at gmail.com>
> To: tutor at python.org
> Subject: [Tutor] Debugging While Loops for Control
> Message-ID: <A8BDF988-FE78-4CA1-8CB7-C0A0E68FDCB5 at gmail.com>
> Content-Type: text/plain; charset=us-ascii
> I've been translating and extending the Blackjack project from codeacademy.com into Python. My efforts so far are here: https://gist.github.com/1842131
> 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. If so, is there an elegant alternative still running the functions at least once.
> while ask_for_raw_input() AND is_the_score_over_21():
> Any advice or observations are appreciated, but please don't solve the whole puzzle for me at once! And no, not all the functionality of a real game is implemented. The code is pretty raw. I'm just a hobbyist trying to learn a few things in my spare time.
> Thanks in advance.
> Message: 2
> Date: Thu, 16 Feb 2012 09:05:39 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Debugging While Loops for Control
> Message-ID: <jhigt3$jdp$1 at dough.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 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():
> 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():
> 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.
> Alan G
> Author of the Learn to Program web site
Alan (and list),
Thanks for the advice. It at least points me to an answer: I'm trying to be too clever for my experience level. I am going to go back and incorporate your suggestions.
In the meantime, and continuing my problem of over-cleverness, I was trying to rethink the program in classes. With the caveat that I'm only a few hours into this rethinking, I've added the code below. My question is: when I want to build in a "return self" into the Hand class, which is made up of the card class; how do I force a conversion from card object into integer object which is all the card class is really holding? Should I make the class Card inherit from Integers? or is there a __repr__ def I don't understand yet?
Bonus question: when I create a the "def score(self)" in class Hand, should that be an generator? And if so where do I go as a newb to understand generators? I'm really not understanding them yet. The "x for x in y:" syntax makes it harder to follow for learners, even if I appreciate brevity.
Thanks in advance,
self.score = self.deal()
"""deal a card from 1 to 52 and return it's points"""
return self.getValue(int(math.floor(random.uniform(1, 52))))
def getValue(self, card):
"""Converts the values 1 - 52 into a 1 - 13 and returns the correct blackjack score based on remainder."""
if (card % 13 == 0 or card % 13 == 11 or card % 13 == 12):
#Face Cards are 10 points
elif (card % 13 == 1):
#Regular cards, return their value
return card % 13
self.cards = 
#Add cards this way to avoid duplicates.
for i in range(2):
#how do you sum(objects) ???
More information about the Tutor