+ 1

count characters

Yes it is my homework. I have to write a program the coounts the occurency of each character of a given word. word = input("please enter a word") Georg for c in word: #now lets say c is G G=1 ... E=1 O=1 R=1.. but now G=1 is wrong and how can I check that I already have a variable G?

6th May 2020, 9:12 AM
GeorgeT
15 Antworten
+ 2
Using dictionary to keep count of how many times a variable appeared in a word
6th May 2020, 9:19 AM
Abhay
Abhay - avatar
+ 3
To get the appropriate result, string method “upper()” should be used to get G = 2 and not G = 1 , g = 1
8th May 2020, 2:21 AM
Jibril Balogun
Jibril Balogun - avatar
+ 1
wait... my effort... but it is exactly that
6th May 2020, 9:15 AM
GeorgeT
+ 1
Abhay I needed the hint to do it with dictionary. With a cheatsheet on my desktop I can code simple programs. If one tells me to do with a list, I can often do it. But list,set,dictionary where to use when is a problem to me. Anyways I can do it now. And also that other 4 homeworks. If I have to find all successors characters of a character in a text than it is again a dictionary with key character and value a list..,,right?
6th May 2020, 11:35 AM
GeorgeT
+ 1
yes
6th May 2020, 11:41 AM
Abhay
Abhay - avatar
+ 1
Abhay look it did it.
6th May 2020, 11:41 AM
GeorgeT
+ 1
loop through a set of the word.
6th May 2020, 11:48 AM
rodwynnejones
rodwynnejones - avatar
+ 1
GeorgeT Nice one ,I had one solution but it has way to many lines than that ,ty for showing me other way to do it. :-)
6th May 2020, 11:50 AM
Abhay
Abhay - avatar
+ 1
Use for loop and dict
8th May 2020, 4:26 AM
Abhishek Singh
Abhishek Singh - avatar
0
GeorgeT that's not a try ,you are doing nothing in that code ,it's not that difficult if you know which letter is going to appear in the word ,like count1=0 count2=0 for i in word If i==o: coun1+=1 Elif i==r: count2+=1 but then you have to print seperate variables and know which variable is for which letter ,best is to use dictionary
6th May 2020, 10:16 AM
Abhay
Abhay - avatar
0
rodwynnejones oh ...but why does it not show an error? I have G two times. https://code.sololearn.com/cxcK635842F2/?ref=app
6th May 2020, 11:50 AM
GeorgeT
0
Abhay lol... you are welcome. i tried count[i] +=1 because I didn't understand fully your code but got a key error. That was my idea for that.
6th May 2020, 11:59 AM
GeorgeT
0
As i mentioned previously... word = input("please enter a word") for x in set(word): print(x, '=', word.count(x), end=', ')
6th May 2020, 4:39 PM
rodwynnejones
rodwynnejones - avatar
0
Dr. Exceptional yes good point.
8th May 2020, 8:40 PM
GeorgeT