0

Trying to set up a function that counts the number float values contain inside a txt file when exceeds a custom limit:

count_out = 0 
def testing(txts, limit, count_in):
   #tried global but doesn't work
   global count_out
   
   text = text.strip()
   values = float(text)
   
   if values > limit:
     count_in += 1
   return count_in

However currently, it doesn't count the number of values pass through the limit and remain at default value of 0 :

for a in open('sample.txt'):
  testing(a, 35, count_out)

print(count_out)

The values within the sample.txt are:

28.8
49.5
29.0
27.6
35.7
3
  • Although you declare count_out to be global, you don't modify it inside testing() Commented Nov 7, 2020 at 12:09
  • You can't pass immutable objects like integers as an argument "by reference". So passing count_out and changing it in the function won't change the contents of the original variable define outside the function (whether you declare it global or not). Commented Nov 7, 2020 at 12:24
  • Off-topic. The variable text is undefined in the code in your question and call the function will result in an UnboundLocalError: local variable 'text' referenced before assignment. Commented Nov 7, 2020 at 12:25

2 Answers 2

1

Perhaps you meant to pass count_out into your function and update it:

count_out = 0 

def testing(text, limit, count_in):
   text = text.strip()
   values = float(text)
   
   if values > limit:
     count_in += 1
   return count_in

for a in open('sample.txt'):
    count_out = testing(a, 35, count_out)

print(count_out)

Output: 2

Sign up to request clarification or add additional context in comments.

6 Comments

Tried this already, the count_out doesn't count. That's why attempted to use global.
What does doesn't count mean? Actually I'm getting an error, so I'll fix that....
count_out stayed at "0" instead of adding count_in values
Code fixed and it outputs 2. That sounds about right for your sample.txt
@quamrana I get this error? could not convert string to float: '28.8 ..? Any idea why?
|
0

Good day. This code worked fine.

count_out = 0 
def testing(txts, limit, count_in):
   #tried global but doesn't work
   global count_out
   text = txts.strip()
   values = float(text)

   if values > limit:
     count_out += 1 #change to global name
   return count_in # it return statement not nessesary

for a in open('sample.txt'):
   testing(a, 35, count_out)

print(count_out)

My variant

count_out = 0 
def testing(txts, limit):
   #tried global but doesn't work
   global count_out
   text = txts.strip()
   values = float(text)

   if values > limit:
     count_out += 1

for a in open('sample.txt'):
   testing(a, 35)

print(count_out)

2 Comments

Your code, as posted, can't possibly work because it has the same error my answer had before I fixed it.
The code in the OPs question and yours have an unbound local variable error — so saying it "worked fine" could not possibly be true,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.