>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 15 Feb 2012 23:57:08 -0500
> From: Luke Thomas Mergner <[email protected]>
> To: [email protected]
> Subject: [Tutor] Debugging While Loops for Control
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=us-ascii
>
> Hi,
>
> 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.
>
> e.g.
> while ask_for_raw_input() AND is_the_score_over_21():
> hitMe(hand)
>
>
> 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.
>
> Luke
>
> ------------------------------
>
> Message: 2
> Date: Thu, 16 Feb 2012 09:05:39 +0000
> From: Alan Gauld <[email protected]>
> To: [email protected]
> Subject: Re: [Tutor] Debugging While Loops for Control
> Message-ID: <[email protected]>
> 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():
>> 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/
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,
Luke
class Card(object):
def __init__(self):
self.score = self.deal()
def deal(self):
"""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
return 10
elif (card % 13 == 1):
return 11
else:
#Regular cards, return their value
return card % 13
def showCard(self):
return repr(self.score)
class Hand:
def __init__(self):
self.cards = []
#Add cards this way to avoid duplicates.
for i in range(2):
self.cards.append(Card())
def hit(self):
self.cards.append(Card())
def showHand(self):
return self
def score(self):
#how do you sum(objects) ???
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor