1

So I think I am almost there. I am searching a poker hand history txt document counting the number of hands.

At the start of each hand we have this statment "Game #xxxxxx starts."

I am looking the word "starts." present at the start of each hand so I wanted to count all the occurrences of this.

Hands_Played is always returning as 0 so I think the word I am looking for is not being picked up properly

Any ideas what I'm doing wrong?

Hands_Played = 0

def CountHands(Hands_Played):
    Hand_History_File = open("C:\xxxxx\blah_blah.txt", "r")
    data = Hand_History_File.read()
    print data
    print "Counting Hands..."
    for line in data:
        print "Loop" #Shows me that each line is being read
        if "starts." in Hand_History_File:
            Hands_Played = Hands_Played + 1
    return Hands_Played            

CountHands(Hands_Played)
print "Number of Hands Played: %s" % Hands_Played
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
Behzad
  • 123
  • 1
  • 1
  • 11
  • Careful, you want to use a raw string with `open()`. In your dummy example, your string would have been interpreted as `C:\xxxxxlah_blah.txt`. Or use forward slashes instead of backslashes. – Tim Pietzcker Jul 23 '12 at 13:31
  • There are many things wrong with this code, so it does not function well as a duplicate. Recommend deletion; the question definitely does not conform to 2022 standards. – Karl Knechtel Sep 11 '22 at 07:11

4 Answers4

1

Update:

You also need to assign the return value from the function to a variable before you can use it subsequently:

CountHands(Hands_Played)
print "Number of Hands Played: %s" % Hands_Played

Should be

Hands_Played = CountHands(Hands_Played)
print "Number of Hands Played: %s" % Hands_Played

Otherwise you never receive the count, and you just end up displaying the initial value you set at the start of the program (0).

For that matter, unless you are maintaining a count between function calls, you don't really need a parameter for this function. Just create the Hand_Played variable locally inside, at the start of the function, and return it as you do now. You can remove the parameter from your function header and your call and you should be all set.

--- previous, still applies.

Instead of:

if "starts." in Hand_History_File:

you probably want

if "starts." in line:

Also, don't forget to explicitly close the file when you are done with it. I.e., make a call Hand_History_File.close() before you return from the function.

Better yet, consider using the with construct to manage you files. It will close your files for you automagically at the end of the block when you are done with it, or you encounter an exception. And you could also process your file line-by-line and simplify your code some more. I.e., a streamlined version, close to your original design of your function/code:

def CountHands():
    Hands_Played = 0
    with open(r"C:\xxxxx\blah_blah.txt") as Hand_History_File:
       for line in Hand_History_File:  # process file line-by-line
           if "starts." in line:
              Hands_Played +=  1
    return Hands_Played


Hands_Played = CountHands()
print "Number of Hands Played: %s" % Hands_Played

Notes:

I use a "raw" string (with the 'r' in front of your path) to prevent Python from interpreting certain characters combinations the string. I don't use "r" in the open call as it's the default when opening files. I use the += operator to shorten your increment. And your file is closed when you leave the block started with with.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Levon
  • 138,105
  • 33
  • 200
  • 191
  • Thanks for the quick reply, I am still getting a return of 0 though. Am I using the string incorrectly for the search? – Behzad Jul 23 '12 at 13:30
  • wouldn't this iterate over chars instead of lines? – zenpoy Jul 23 '12 at 13:31
  • @zenpoy: No, with strings, the `in` operator looks for substrings. – Tim Pietzcker Jul 23 '12 at 13:31
  • @TimPietzcker are you sure? `for s in "hello\nthere!": print s` – zenpoy Jul 23 '12 at 13:38
  • @zenpoy: That's a different use of the `in` operator, you're right. For iteration, it uses characters, for membership testing, substrings. – Tim Pietzcker Jul 23 '12 at 13:39
  • @TimPietzcker , @Levon - I was referring to `for line in data:` part. since data is just a string , no? `data = Hand_History_File.read()` – zenpoy Jul 23 '12 at 13:43
  • Thanks for all this Levon, I now have a successful count but its counting individual characters in "start" e.g all the s, t, a and r chars in the .txt – Behzad Jul 23 '12 at 13:52
  • How can I make sure it only recognises start as an entire word? – Behzad Jul 23 '12 at 13:53
  • @Levnon hmmm ok it's still not but I think I'll start again fresh and try to remember all of this and see if I can tidy it up and get a value coming out – Behzad Jul 23 '12 at 14:11
  • @Behzad I tested the code I posted with a sample file I created, and it worked, so you can just copy and paste it and it should work. The only problem I can think of is a difference in the string you specify in your program and what's in the file. – Levon Jul 23 '12 at 14:16
1

why not let python do the counting itself ?

def CountHands(Hands_Played):
    with open("C:\xxxxx\blah_blah.txt", "r") as Hand_History_File:
        Hands_Played += Hand_History_File.read().count("starts.")
    return Hands_Played            

What you're doing wrong in your code, is :

  1. you should use the if "starts." in Hand_History_File, here you check the existence of "starts." in your file handle, not in the content of the file

  2. you use the same variable name as a global and parameter name. Thus, the parameter modified in the function is not the global one, only a copy of it. To correct this try to print what your CountHands() call returns

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • This would probably work better but why does what I wrote not count properly? – Behzad Jul 23 '12 at 13:34
  • @Behzad : you're right, I forgot to answer the real question ;) I edited my answer – Cédric Julien Jul 23 '12 at 13:39
  • So close! but after making these changes I've realised its adding +1 for each character present in "start." How do I ensure that "start." is taken as an entire string which shouldn't be separated? – Behzad Jul 23 '12 at 13:46
  • @Behzad : the `count("starts")` should already count only the presence of the whole "starts" word... – Cédric Julien Jul 23 '12 at 14:00
1

The problem is that you are not using scope correctly. The problem is that python is interpreting the Hands_Played that the function takes in and the Hands_Played outside the function as different variables. Look at the code below:

>>>variable = 10
>>>def addOne(variable):
    variable += 1
    return variable

>>>variable
10
>>>addOne(variable)
11
>>>variable
10

You can fix this by replacing CountHands(Hands_Played) with the statement Hands_Played = CountHands(Hands_Played).

Community
  • 1
  • 1
Rob Volgman
  • 2,104
  • 3
  • 18
  • 28
  • Sure. I added a short explanation, but basically the function you write is not actually changing the `Hands_Played` variable that you defined outside the function. It is changing a variable internal to the function. A variable that a function takes as an input is considered local - you could use `x` instead, and the function would act identically. If you instead used `def CountHands():` (with no input variable), the function would have used the global version of `Hands_Played`. – Rob Volgman Jul 23 '12 at 13:41
0

This is how you iterate over a file:

with open('workfile') as f:
    for line in f:
        print line,
        # do something
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
zenpoy
  • 19,490
  • 9
  • 60
  • 87