Wednesday, 14 August 2013

Using Counters and Lists in Python?

Using Counters and Lists in Python?

this is my program:
msg = input("What is your message? ")
print ()
num_alpha = 26
int_array = [0] * num_alpha
for alpha in range(num_alpha):
int_array[alpha] = chr(alpha + 65)
print(int_array[alpha], end = "")
print()
lett = 0
otherch = 0
num_vowels = 0
num_consanants = 0
count_character = [0] * 100000
length = len(msg)
for character in msg.upper():
if character == "!":
print("lett =", lett)
print("other char = ", otherch)
print("num_vowels = ", num_vowels)
print("num_consanants = ", num_consanants)
elif character < "A" or letter > "Z":
otherch = otherch + 1
count_character[ord(character)] = count_character[ord(character)] + 1
print(count_character[ord(character)])
else:
lett = lett + 1
count_character[ord(character)] = count_character[ord(character)] + 1
print(count_character[ord(character)])
for character in msg:
print("character", character, "appeared" ,
count_character[ord(character)] , "times")
i'm meant to be listing various features about the message (how many times
each letter appeared, number of vowels, number of non-letter characters,
etc.) but every time i print the last print statement it states that each
character appeared 0 times. any helpers?
EDIT: I've figured it out -
Because of the previous loop, rather than:
for character in msg:
print("character", character, "appeared" ,
count_character[ord(character)] , "times")
The code needs to be:
for character in msg.upper():
print("character", character, "appeared" ,
count_character[ord(character)] , "times")

No comments:

Post a Comment