0
Write a python program to read a text file "story.txt" and display the number of alphabets, vowels, consonants, and special cha.
X=open("story.txt","r+") a=v=d=c=sp=0 r=f.read() Print("contents of file is":,r) for I in r: If i.isalpha(): a+=1 elif i=='AEIOU aeiou': v+=1 elif i.isdigit(): d+=1 elif i != 'AEIOU aeiou': C+=1 else : What to do for display of a special characters?? After else????
3 Antworten
+ 3
The problem with the code, from what I can see (please add it as per the attached guide - it helps us to help you), you only count your "isalpha" or vowel, or number, or not a vowel.
You probably want the indentation and if/elifs a bit differently:
If isalpha:
Code
If vowel:
Code
Elif digit:
Code
Elif consonant:
Code
Else:
Code
Does that make sense?
https://sololearn.com/compiler-playground/Wek0V1MyIR2r/?ref=app
+ 2
Betsy Mary Biju ,
you are counting
> vovels
> consonants
> digits
> special characters
but what are you counting for 'alphabets`?
+ 2
Not the subject of the post but you could optimize your file handling if you use with statements
with open("story.txt","r+") as story:
r = story.read()
With this way there is no need to run the close() method.
Also watch out, your file is opened in your example as X, but the file read is referenced as "f"
Edit: See https://www.sololearn.com/en/compiler-playground/cDjvBxZ1Ror for my take on this