Python: Code is ignoring the if and else

Dave Angel davea at davea.name
Fri Aug 2 23:34:56 EDT 2013


kevin4fong at 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.
>
   <snip>

Please list the program the way you are actually running it.  The
present one will not run very long before producing the error:

Traceback (most recent call last):
  File "kevin.py", line 100, in <module>
    temp_hit = player_0_hitman(hit)
NameError: name 'player_0_hitman' is not defined

Next, tell us the environment it runs in.  In this case that means the
version of Python you're using.  In some places you include parens
around the print arguments, like needed in Python 3, but in others your
code could only work up to version 2.7  So we have to do some detective
work just to discover you're using version 2.x

Next, when telling us of an error, quote the exact traceback, don't just
say something like:  "I get a an error saying "card" is not a global"

Next, what on earth does the following mean:
" And if I try to shift it in" ??  Are you perhaps referring to Perl
grammar, or to DOS batch files?

Next, comments like: "tried that and I ended up with a whole bunch of
new errors"  don't tell us much.  Likewise " I'm not even able to start
the program. If I try to change the if statement that it corresponds
with"  The last noun was "program" so I don't know which of the
program's if statements you're changing.

There are multiple things wrong with the code, and you can't expect any
one of us to be able to spot them all.  Some are clear by inspection,
and others would probably require actually running the code, after you
fix the problem with player_0_hitman above.  Example of inspection: you
have the line:
       temp_hit = player_0_hitman(hit)

but never actually use the temp_hit variable.

Similarly, the line:
    pHands[target_player].remove(card)

is going to mess up the loop it's in,  When you're iterating over a
list, you can't normally add or remove items from that list.

Even after you fix that, the loop is still all wrong.  The count method
will never get more than one, since all the cards are unique.  You want
to count up the cards that match a particular rank, regardless of suit. 
And that loop effectively does that, transferring one at a time.  But
you don't do anything to keep track of how many you transferred.

As for the misplaced else, it doesn't belong there at all.  What you
presumably want to do is after exiting the loop, you want to check the
corrected count value to decide how many cards were transferred.  If
that is non zero, then the logic should be executed, including
decrementing the hit value and returning it.


 Is this your own program? If so, you should be able to
siimplify it to ask a question. Few of us are willing to figure out
the logic of go-fish, but many of us are willing to figure out what
indentation makes sense for a given function.

-- 
DaveA




More information about the Python-list mailing list