Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51833
| Date | 2013-08-03 02:38 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Python: Code is ignoring the if and else |
| References | <c6702eb3-494a-4359-84ef-d6679c09df35@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.139.1375493890.1251.python-list@python.org> (permalink) |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-02 17:40 -0700
Re: Python: Code is ignoring the if and else MRAB <python@mrabarnett.plus.com> - 2013-08-03 02:38 +0100
Re: Python: Code is ignoring the if and else John Ladasky <john_ladasky@sbcglobal.net> - 2013-08-02 18:39 -0700
Re: Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-02 18:44 -0700
Re: Python: Code is ignoring the if and else Chris Angelico <rosuav@gmail.com> - 2013-08-03 02:56 +0100
Re: Python: Code is ignoring the if and else Joshua Landau <joshua@landau.ws> - 2013-08-03 03:11 +0100
Re: Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-02 19:24 -0700
Re: Python: Code is ignoring the if and else Terry Reedy <tjreedy@udel.edu> - 2013-08-03 01:04 -0400
Re: Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-03 13:12 -0700
Re: Python: Code is ignoring the if and else Terry Reedy <tjreedy@udel.edu> - 2013-08-02 21:42 -0400
Re: Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-02 18:46 -0700
Re: Python: Code is ignoring the if and else Dave Angel <davea@davea.name> - 2013-08-03 03:34 +0000
csiph-web