How to Calculate the Sum of Values from a File in Python Without Using sum()?

0
2
Asked By CuriousCoder42 On

I'm trying to figure out how to extract data from a `.txt` file where I need to filter based on certain keywords, then convert those values to floats. My goal is to calculate a total sum as **(number of data points / total sum of values)**, but the twist is that the problem specifically prohibits the use of the `sum()` function. Am I missing something? I'd love to hear your suggestions on how to tackle this! If you want, I can also share a possible approach for solving this. Let me know your thoughts!

3 Answers

Answered By TechWiz85 On

Are you sure you can't use other methods for accumulating values? If you can leverage lists or even a simple loop, you should get the right result without `sum()`. Just break down your problem, it can be done!

Answered By SkepticalBee99 On

You could just use a for loop to add up the values manually. Read through the file, filter for your keywords, convert the numbers to floats, and keep a running total. Then you can divide by the count of those numbers to get your final result!

Answered By CodingNinja37 On

Is this for a homework assignment? If so, I get why you're avoiding `sum()`, but don't worry, there are plenty of ways to get the sum using loops. Just iterate through the data, and filter while you go!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.