You are not logged in.
Hi! Today I was learning python and I recently found out that my while loop in python is not working.
I installed python by doing -
sudo pacman -S python python3
Then I made a file in "/home/rounak/Desktop/hello1.py"
myfile = open("sample.txt" , "r")
ch=""
vcount = 0
ccount = 0
while ch:
ch = myfile.read(1)
print(ch)
if ch in ['a','A','e','E','i','I','o','O','u','U']:
vcount = vcount + 1
else:
ccount = ccount + 1
print("Vowels in the file:" , vcount)
print("Consonants in the file:", ccount)
ch = myfile.read(1)
print(ch)
myfile.close()
In the same directory there is a sample.txt file "/home/rounak/Desktop/sample.txt"
Hello I am testing python
Python is so easy
but when I run the hello1.py file I get
[rounak@archissexy Desktop]$ python hello1.py
Vowels in the file: 0
Consonants in the file: 0
H
You will see the ch = myfile.read(1) which is on the third last line prints the first chracter 'H' of Hello but the whole while loop gets ignored if u look closely.
So what am I doing wrong ? I just installed the python package so do I need to set some environment variables or do I need to configure it to work properly ? There were other program too where I did while and while is just not working.
Last edited by RounakDutta (2022-05-30 16:14:49)
Offline
Because "ch" starts empty. This has nothing to do w/ archlinux, python ot the environment. The code is bogus.
Try
ch=" "
Offline
Because "ch" starts empty. This has nothing to do w/ archlinux, python ot the environment. The code is bogus.
Try
ch=" "
Oh my god thank you so much!!!! It works now !!
After changing it to
myfile = open("sample.txt" , "r")
ch=" "
vcount = 0
ccount = 0
while ch:
ch = myfile.read(1)
if ch in ['a','A','e','E','i','I','o','O','u','U']:
vcount = vcount + 1
else:
ccount = ccount + 1
print("Vowels in the file:" , vcount)
print("Consonants in the file:", ccount)
myfile.close()
When I do python hello.py
Vowels in the file: 12
Consonants in the file: 34
It works !!!!
Marking the post as [SOLVED]
Offline
That's a pretty liberal defintion of "works". It reports 34 consonants in the file when there are actually only 21. But this is on your code not actually implementing the logic you seem to intend. Spaces are not consonants. Newlines are not consonants.
Also, FYI:
...
str = myfile.read()
vowels = ['a','A','e','E','i','I','o','O','u','U']
print("Vowels in the file:", sum([ str.count(x) for x in vowels ]))
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
That's a pretty liberal defintion of "works". It reports 34 consonants in the file when there are actually only 21. But this is on your code not actually implementing the logic you seem to intend. Spaces are not consonants. Newlines are not consonants.
Oh I didn't know that.
Well I am still a noob in python
Also, FYI:
... str = myfile.read() vowels = ['a','A','e','E','i','I','o','O','u','U'] print("Vowels in the file:", sum([ str.count(x) for x in vowels ]))
Thanks!!
Added it to my python file .
Last edited by RounakDutta (2022-05-30 18:47:42)
Offline
... str = myfile.read() vowels = ['a','A','e','E','i','I','o','O','u','U'] print("Vowels in the file:", sum([ str.count(x) for x in vowels ]))
Just to improve upon this very slightly:
str = myfile.read().lower()
vowels = 'aeiou'
print('Vowels in the file:', sum(str.count(x) for x in vowels))
1. May as well compare string lowercase to keep definition list simpler.
2. No need to create a temporary list for the sum(). Just use "on-the-fly" generator.
3. Choose single or double quotes for Python strings and then use consistently.
Last edited by bulletmark (2022-05-30 22:24:43)
Offline