1

Here is my code. I'm having trouble figuring out how to make my program count non alphanumerics. I am a new coding student so go easy on me.

infile = open("Gettysburg.txt", "r")
data = infile.readlines()
non_alpha_num = 0
uppercase_count = 0
lowercase_count = 0
whitespace_count = 0
digit_count = 0
for character in data:
    if character.isupper():
        uppercase_count += 1
    elif character.islower():
        lowercase_count += 1
    elif character.isspace():
        whitespace_count +=1
    elif character.isdigit():
        digit_count +=1
    if not character.isalnum() and not character.isspace():
        non_alpha_num += 1
    print("Jake's text document counter")
    print('The uppercase count is ', uppercase_count)
    print('The lowercase count is ', lowercase_count)
    print('The digit count is ', digit_count)
    print('The whitespace count is ', whitespace_count)
    print('The non alphanumeric count is ', non_alpha_num)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jake
  • 25
  • 5
  • I'm able to have my program count and display the amounts for digits, uppercase, etc. but I don't know how to count the non alpha numeric characters. – Jake Apr 11 '18 at 00:22
  • You could try refrencing an array of non alphanumeric chars using the `in` operator, but it seems that any character that doesn't pass one of these tests is by default a part of the non alphanumeric set, which is basically what Sruthi said. – ThisGuyCantEven Apr 11 '18 at 00:23
  • 1
    This might be a copy-paste issue, but you have an indentation problem: the code inside the last elif should be indented. (If those prints aren't meant to be in the elif, then you have an empty elif, which is also a problem.) – Niayesh Isky Apr 11 '18 at 00:26
  • Yea my code is unfinished so I'm not too worried about indentation currently. I deleted that last elif line to avoid confusion. – Jake Apr 11 '18 at 00:29
  • Note that whitespace is non-alphanumeric, so it'll show up twice. Is that OK, or do you need the totals to add up to the total character count? – abarnert Apr 11 '18 at 00:34
  • if you need to exclude the whitespaces, just subtract that sum from the sum you get from @Sruthi V's answer – ThisGuyCantEven Apr 11 '18 at 00:37
  • 1
    Umm... You're not iterating characters. You used `readlines` to initialize data; your loop is over lines, not characters. – ShadowRanger Apr 11 '18 at 00:43
  • I figured it out thanks to all your guys help. Thank you all very much! – Jake Apr 11 '18 at 01:03

1 Answers1

4

Try

if not character.isalnum():
    non_alpha_num += 1

To exclude whitespaces :

if not character.isalnum() and not character.isspace():
    non_alpha_num += 1

EDIT : Following @ShadowRanger comment : You are not reading characters, you are reading lines. Please modify your code.

infile = open("Gettysburg.txt", "r")
data = infile.readlines()

uppercase_count=0
lowercase_count=0
whitespace_count=0
digit_count=0
non_alpha_num=0

for line in data:
    for character in line :
        if character.isupper():
            uppercase_count += 1
        elif character.islower():
            lowercase_count += 1
        elif character.isspace():
            whitespace_count +=1
        elif character.isdigit():
            digit_count +=1
        elif not character.isalnum() and not character.isspace():
            non_alpha_num += 1


print("Jake's text document counter")
print('The uppercase count is ', uppercase_count)
print('The lowercase count is ', lowercase_count)
print('The digit count is ', digit_count)
print('The whitespace count is ', whitespace_count)
print('The non alphanumeric count is ', non_alpha_num)
Sruthi
  • 2,908
  • 1
  • 11
  • 25