Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This seems to be what you are trying to do in <code>finder_character</code>. I'm not sure why you need <code>finder</code> at all.</p> <p>In python you can loop over iterables (like strings), so you don't need to do <code>range(len(string))</code>.</p> <pre><code>for line in in_file: for i in brain_rat: if i in line: found += 1 </code></pre> <p>There appear to be a few other oddities in your code too:</p> <ul> <li>You open (and iterate through) the file twice, but only closed once.</li> <li><code>line_number</code> is never used</li> <li>You get the total of a character in a file for each line in the file, so the total will be vastly inflated.</li> </ul> <p>This is probably a much safer version, <code>with open...</code> is generally better than <code>open()... file.close()</code> as you don't need to worry as much about error handling and closing. I've added some comments to help explain what you are trying to do.</p> <pre><code>def finder_character(file_name,character): found=0 # Initialise the counter with open(file_name, "r") as in_file: # Open the file in_file = file_name.split('\n') opts = { 'vowel':'aeiou', 'consonant':'bcdfghjklmnpqrstvwxyz', 'space':'' } default= '!@#$%^&amp;*()_+=-123456789{}|":?&gt;&lt;,./;[]\'' for line in in_file: # Iterate through each line in the file for c in opts.get(character,default): With each line, also iterate through the set of chars to check. if c in line.lower(): # If the current character is in the line found += 1 # iterate the counter. return found # return the counter </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload