#!/usr/bin/env python3 # -*- coding: utf-8 -*- def odd_even_counter(in_file, count_letter): '''Function: Get a number of symbols if it even or odd in any text file''' odd_counter = 0 even_counter = 0 with open(in_file, 'r') as f0: phrase_to_check = ''.join([x for x in f0.read() if x.isalpha()]) for position, item in enumerate(phrase_to_check): if item == count_letter: if (position + 1) % 2 == 1: odd_counter += 1 else: even_counter += 1 print('Π‘ΠΈΠΌΠ²ΠΎΠ» [', count_letter, '] Π½Π° Π½Π΅Ρ‡Π΅Ρ‚Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ:', odd_counter, 'Ρ€Π°Π·') print('Π‘ΠΈΠΌΠ²ΠΎΠ» [', count_letter, '] Π½Π° Ρ‡Π΅Ρ‚Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ:', even_counter, 'Ρ€Π°Π·') if __name__ == '__main__': test_file = 'lorem_ipsum.txt' # любой тСкстовый Ρ„Π°ΠΉΠ» test_letter = 'o' # любой символ odd_even_counter(test_file, test_letter) 
  • one
    it would be useful to add a verbal description of what the code should do to make sure that it works correctly. - jfs

1 answer 1

It is necessary to separate calculations and input / output - such code is simpler, it is easier to test and reuse.

Calculations:

 from collections import namedtuple Counters = namedtuple('Counters', 'even odd') def odd_even_counter(text, char): """How many times *char* is in even/odd positions in *text*. Indexing starts with one. """ counters = [0, 0] # even, odd for position, current_char in enumerate(text, start=1): if current_char == char: counters[position % 2] += 1 return Counters(*counters) 

Example:

 >>> odd_even_counter('abbb', 'b') Counters(even=2, odd=1) 

Input Output:

 with open('lorem_ipsum.txt') as file: letters = (c for line in file for c in line if c.isalpha()) char = 'o' count = odd_even_counter(letters, char) print('Π‘ΠΈΠΌΠ²ΠΎΠ» [ {char} ] Π½Π° Π½Π΅Ρ‡Ρ‘Ρ‚Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ: {count.odd} Ρ€Π°Π·'.format(**vars())) print('Π‘ΠΈΠΌΠ²ΠΎΠ» [ {char} ] Π½Π° Ρ‡Ρ‘Ρ‚Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ: {count.even} Ρ€Π°Π·'.format(**vars())) 

If necessary, the definition and calculation of letters (casefold (), NFKD, grapheme clusters) can also be put into a separate function.
The code reads one line at a time, instead of the entire file.

Result

 Π‘ΠΈΠΌΠ²ΠΎΠ» [ o ] Π½Π° Π½Π΅Ρ‡Ρ‘Ρ‚Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ: 20 Ρ€Π°Π· Π‘ΠΈΠΌΠ²ΠΎΠ» [ o ] Π½Π° Ρ‡Ρ‘Ρ‚Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ: 18 Ρ€Π°Π·