+ 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?
15 Antworten
+ 2
Using dictionary to keep count of how many times a variable appeared in a word
+ 3
To get the appropriate result, string method “upper()” should be used to get G = 2 and not G = 1 , g = 1
+ 1
wait... my effort... but it is exactly that
+ 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?
+ 1
yes
+ 1
Abhay look it did it.
+ 1
loop through a set of the word.
+ 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. :-)
+ 1
Use for loop and dict
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
0
rodwynnejones oh ...but why does it not show an error?
I have G two times.
https://code.sololearn.com/cxcK635842F2/?ref=app
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.
0
As i mentioned previously...
word = input("please enter a word")
for x in set(word):
print(x, '=', word.count(x), end=', ')
0
Dr. Exceptional yes good point.