Hi, I’m trying to write out a code on Coderbyte that counts the number of consonants in a string, I know I must be missing something but could someone explain where I’m going wrong? I’ve changed it a few times but at this rate I don’t want to make it any worse I’m also not sure what to do with the raw_input? I’ve tried adding “Alphabets” to it but maybe I didn’t do it right?
vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
strParam = “Alphabets”
consonant_count = 0
for char in strParam:
if char in vowels:
consonant_count += 1
Edit: is this any better? I’ve managed to condense it down a bit but it’s still throwing errors back at me? (says consonant_count is not defined)
strParam = “Alphabets”
consonant_ count = 0
for i in strParam:
if i not in “aeiouAEIOU”:
consonant_ count += 1
print (“Number of consonants: “ + str(consonant_count))
Your print() statement is outside of the function, meaning that it’s outside of the scope where consonant_count is defined. Additionally, by setting strParam to "Alphabet" (i.e., by assigning it a value inside the function), there’s little point in having it as a parameter; if you wanted to accept user input from within the function, you don’t need to have it as a parameter…
A few minor things
There seems to be a space between consonant_ and count where you declare that variable:
Also, you’ve named your function in PascalCase (where you capitalise every word’s first letter), but you should use snake_case (where you put an underscore between each word). Same goes for strParam; you’ve used camelCase, but should use snake_case…
Thank you both! As for the PascalCase, it was the way the code was set up for the challenge before I did anything to it so I wasn’t sure if I was supposed to change it at all.
Same goes for the print statement, I didn’t change where it was indented (or not indented) so I didn’t realise that’s what was throwing off my code, as well as the fact that it didn’t have brackets around it originally (only around raw_input).
It also makes so much sense to me now about where my return function should’ve been so thank you for clarifying! I can see I had the right idea with my code but it’s just little tweaks I had to make
P.S. the space in consonant_count wasn’t there in my code, I just wrote it out on here with my iPad and autocorrect got in the way but helpful to know!