jason
jason

Reputation: 23

Program is repeating itself -can't figure out why

This is my program so far...it takes a message (input from user) and tells the user how many A's are in the program, how many B's, etc. Except when I input a message such as "Dad", it'll tell me how many D's there are twice instead of just saying everything once. It says:

D ... 2

A ... 1

D ... 2

I want it to say:

A ... 1

D ... 2

How do I fix this without using zip, and without importing anything?

message=input("what is your message?").upper()
alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
count=[0]*len(alphabet)
for i in message:
    if i in alphabet:
        count[alphabet.index(i)]+=1

for i in message:
    print (i,"...",count[alphabet.index(i)])

(Thanks to Uriel Eli for helping me get the program this far btw).

Upvotes: 0

Views: 54

Answers (3)

idjaw
idjaw

Reputation: 26570

I don't agree with your approach here. You have actually over complicated this. The proper way to solve this is to actually use a dictionary to keep track of all the letters in the string and keep a count every time the same character comes up. Note, this also sticks to the rule of not importing anything.

Furthermore, this removes the necessity to have a list of letters to check against as well.

Also, if you need to count upper and lower case characters separtely, do not call upper at the end of your input. Just remove it. If you have to count upper and lower case as the same character, then you can leave it.

message=input("what is your message?").upper()

d = {}

for c in message:
    if c in d:
        d[c] += 1
    else:
        d[c] = 1

Demo

what is your message?thisisastringofthings
{'H': 1, 'F': 0, 'O': 0, 'R': 0, 'G': 1, 'S': 3, 'T': 2, 'A': 0, 'I': 3, 'N': 1}

To provide an output similar to what you are expecting, you just need to iterate through your final result and print:

for character, count in d.items(): print("{} ... {}".format(character, count))

Finally, just for the sake of showing the best way to do this, is to actually use Counter from collections:

>>> from collections import Counter
>>> Counter("thisisastring")
Counter({'s': 3, 'i': 3, 't': 2, 'h': 1, 'n': 1, 'a': 1, 'r': 1, 'g': 1})

Upvotes: 2

Marcin
Marcin

Reputation: 238309

Just for future reference, and I know that you CANT import anything now. The best way probably would be:

from  collections import Counter
message=input("what is your message?").upper()
print(Counter(message))
# Counter({'D': 2, 'A': 1})

Upvotes: 1

Savir
Savir

Reputation: 18418

Your second for loop is iterating through message, so if the user input DAD (well... after upper casing it), you're gonna get:

message == DAD
  i = D --> Shows 2
  i = A --> Shows 1
  i = D --> Shows 2 (again)

Maybe you'd want to iterate through count, keeping the index that you are iterating (to use it latter to match it with the alphabet list). Something like that:

for index, num_occurences in enumerate(count):
    if num_occurences > 0:
        print("Match found at i=%s which corresponds with alphabet[%s]=%s" %
              (index, index, alphabet[index]))
        print(alphabet[index], "...", num_occurences)

You should check what enumerate does.

If you still want to iterate through message, you can do it, keeping track of what letter did you already display using an auxiliary set (so you don't show the same letter again)

already_shown_letters = set()
for i in message:
    if i not in already_shown_letters:
        print (i,"...",count[alphabet.index(i)])
        already_shown_letters.add(i)

Upvotes: 0

Related Questions