Ignoring anything else that may or may not be correct with your "revised code", the issue causing the error currently quoted in your question is caused by calling the "count" function with an undefined variable because your didn't quote the string.
count(thisisastring222)
looks for a variable called thisisastring222 to pass to the function called count. For this to work you would have to have defined the variable earlier (e.g. with thisisastring222 = "AStringWith1NumberInIt."
) then your function will do what you want with the contents of the value stored in the variable, not the name of the variable.count("thisisastring222")
hardcodes the string "thisisastring222" into the call, meaning that the count function will work with the exact string passed to it.To fix your call to your function, just add quotes around asdfkasdflasdfl222
changing count(asdfkasdflasdfl222)
to count("asdfkasdflasdfl222")
.
As far as the actual question "How to count digits, letters, spaces for a string in Python", at a glance the rest of the "revised code" looks OK except that the return line is not returning the same variables you've used in the rest of the code.
To fix it without changing anything else in the code, change number
and word
to digit
and letters
, making return number,word,space,other
into return digit,letters,space,other
, or better yet return (digit, letters, space, other)
to match current behavior while also using better coding style and being explicit as to what type of value is returned (in this case, a tuple).