Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'subject:Python': 0.06; 'odd': 0.07; 'reason,': 0.07; 'returned.': 0.07; 'definition,': 0.09; 'finished.': 0.09; 'if,': 0.09; 'positioned': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; 'creates': 0.14; '"break"': 0.16; '"for"': 0.16; '"if"': 0.16; 'cards.': 0.16; 'clause,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'ignoring': 0.16; 'luckily': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'sorts': 0.16; 'stumbled': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'basically': 0.19; 'else,': 0.19; 'seems': 0.21; '(the': 0.22; 'appears': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'subject:Code': 0.24; 'why.': 0.24; "i've": 0.25; 'player': 0.26; '(for': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'appreciated.': 0.29; 'skip:p 30': 0.29; 'converting': 0.30; 'errors': 0.30; 'statement': 0.30; "i'm": 0.30; 'work.': 0.31; 'asked': 0.31; 'code': 0.31; 'lines': 0.31; 'this.': 0.32; 'figure': 0.32; 'run': 0.32; 'another': 0.32; 'guess': 0.33; 'subject:the': 0.34; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'definition': 0.35; 'but': 0.35; 'there': 0.35; 'his/her': 0.36; 'returning': 0.36; "didn't": 0.36; 'shows': 0.36; 'changing': 0.37; 'example,': 0.37; 'two': 0.37; 'being': 0.38; 'problems': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; '(from': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'even': 0.60; 'tell': 0.60; 'matter': 0.61; 'first': 0.61; 'back': 0.62; 'email addr:gmail.com': 0.63; 'card': 0.63; 'choose': 0.64; 'more': 0.64; 'cards': 0.65; 'bottom': 0.67; 'header:Reply-To:1': 0.67; 'transferred': 0.68; 'reply-to:no real name:2**0': 0.71; 'fish': 0.84; 'gains': 0.84; 'one).': 0.84; 'player.': 0.84; 'reply-to:addr:python.org': 0.84; 'subject:else': 0.84; 'rank': 0.93; 'hands': 0.96 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=KrN0hwmN c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=K2DDQYBT4xIA:10 a=C-YKEuPJoSsA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=Ba6RBDs3a2sA:10 a=pGLkceISAAAA:8 a=wDHmtW5GjRyjuOV0SQsA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Sat, 03 Aug 2013 02:38:02 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python: Code is ignoring the if and else References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375493890 news.xs4all.nl 15891 [2001:888:2000:d::a6]:41873 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51833 On 03/08/2013 01:40, kevin4fong@gmail.com wrote: > I'm trying to create a game of Go Fish in Python. But I've stumbled onto a little problem that I can't seem to figure out how to deal with. > > There is a human player (player 0) and three computer players (from 1-3). The human player goes first and chooses a target player. And then a card rank (for example, the player could target player two and choose jacks, then the computer would have to give the player all its jacks). > > What I have so far is below but the problem I'm having is right at the bottom of the code. So far, the code generates a deck, creates hands for every player, and then shows the player his/her cards. Then the player is asked which computer player he/she wants to target as well as the rank of cards. > > The problem I'm having is with the last set of lines (the def player_0_hitman) at the bottom of the code. Any help would be much appreciated. There are basically three issues I'm having problems with. > > Basically, my code is ignoring the if's and else's. I don't get why. Everything appears to be positioned correctly, but for some odd reason, even after an if, the program also runs the else as well. > > the "hit" is not being returned. Even though in the definition, I have the hit set to hit = hit - 1 for the last else, it still reruns the whole definition again as if it the hit was 1 > > I'm trying to use the count line to count how many cards are being transferred so the program will tell the player how many cards he gains when he gets a successful guess but I only get a statement saying I got 1 card each time no matter what (whether I get no cards or I get more than one). > > I understand the basics of what I need to do but I can't seem to get a working code for this. I've tried changing the "for" to "if" but I get all sorts of errors so I don't think that will work. Then I tried converting "hit" into another code before entering the definition, changing it while inside, then converting it back before returning it but that also seems to do nothing, and I still get the same issues. > [snip] > def player_0_hitman(hit): > for card in pHands[target_player]: > if target_card[0] == card[0]: > count = pHands[target_player].count(card) > pHands[0].append(card) > pHands[target_player].remove(card) > ShowMessage("HIT: " + str(count) + " card(s) transferred") > else: > if target_card[0] != card[0]: > top_card = GetTopCard(sDeck) > pHands[0].append(top_card) > if top_card[0] == target_card[0]: > ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!") > else: > ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">") > hit = hit - 1 > return hit > You have "else" lined up with "for". In Python a "for" loop can have an "else" clause, which is run if it didn't "break" out of the loop but finished.