0

Help with code python

I took this code from a lesson and changed the letters from a single letter to the whole alphabet in hopes if you entered a word or words you would get the amount of letters in that word So if someone put in “the” it would show the number 3 as in 3 letters but it just shows the number 0 https://code.sololearn.com/cfXh1PK9oTG3/?ref=app

6th Jun 2022, 11:50 PM
jellybeannugget
6 Answers
+ 5
assuming that you are going to count the total number of lower case plus upper case letters. instead of a string with all these letters we can also use the string method .isalpha() together with variable 'x' in the for loop. it returns True for all lower and upper case letters, but not for spaces, numbers or special characters. at the end of the iteration the variable 'count' holds the total number of letters. hint: what we do not need in the input procedure is the converting of the input to string data format, because input returns everything as string by default. this would be ok: text = input() all the rest of your code can remain as it is.
7th Jun 2022, 2:01 PM
Lothar
Lothar - avatar
+ 4
jellybeannugget That's because x == a complete alphabet, so only a complete alphabet will return a True result aÄșlowing a count. You could do something similar to your concept using a dictionary, where the keys are the letters and the values are set to 0. Then as you iterate through your string, you could increment the value of the corresponding key.
7th Jun 2022, 12:04 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Besides Cristian Baeza Jimenez answer, also remove spaces and commas - I understand you want to count only letters.
7th Jun 2022, 11:29 AM
Emerson Prado
Emerson Prado - avatar
+ 2
Rik Wittkopp thanks man helps alot
7th Jun 2022, 12:06 AM
jellybeannugget
+ 2
Change the == for in and it will work
7th Jun 2022, 4:02 AM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
7th Jun 2022, 2:03 PM
jellybeannugget