Advertisements
Advertisements
प्रश्न
The function given below is written to accept a string s as a parameter and return the number of vowels appearing in the string. The code has certain errors. Observe the code carefully and rewrite it after removing all the logical and syntax errors. Underline all the corrections made.
def CountVowels(s):
c=0
for ch in range(s):
if 'aeiouAEIOU' in ch:
c=+1
return(ch)कोड लेखन
Advertisements
उत्तर
def CountVowels(s):
c = 0
for ch in s:
if ch in 'aeiouAEIOU':
c += 1
return c
- Loop Correction: Changed
range(s)tosto iterate through each character of the string. - Membership Logic: Changed
'aeiouAEIOU' in chtoch in 'aeiouAEIOU'to check if the character is a vowel. - Increment Correction: Changed
c=+1(which just assigns 1) toc += 1to properly increment the counter. - Return Correction: Changed
return(ch)toreturn cto return the final count instead of the last character.
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
