You are not logged in.

#1 2022-05-30 15:49:16

RounakDutta
Member
From: West Bengal, India
Registered: 2022-02-11
Posts: 124
Website

[SOLVED] Python while loop not working in arch linux

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)


I am a baller shot caller
I am a arch linux installer
Your proprietary operating system
I wouldn't buy for a dollar

Offline

#2 2022-05-30 16:06:35

seth
Member
Registered: 2012-09-03
Posts: 29,768

Re: [SOLVED] Python while loop not working in arch linux

Because "ch" starts empty. This has nothing to do w/ archlinux, python ot the environment. The code is bogus.

Try

ch=" "

Online

#3 2022-05-30 16:14:25

RounakDutta
Member
From: West Bengal, India
Registered: 2022-02-11
Posts: 124
Website

Re: [SOLVED] Python while loop not working in arch linux

seth wrote:

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]


I am a baller shot caller
I am a arch linux installer
Your proprietary operating system
I wouldn't buy for a dollar

Offline

#4 2022-05-30 17:09:54

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 26,877
Website

Re: [SOLVED] Python while loop not working in arch linux

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

#5 2022-05-30 18:46:25

RounakDutta
Member
From: West Bengal, India
Registered: 2022-02-11
Posts: 124
Website

Re: [SOLVED] Python while loop not working in arch linux

Trilby wrote:

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

Trilby wrote:

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 . smile

Last edited by RounakDutta (2022-05-30 18:47:42)


I am a baller shot caller
I am a arch linux installer
Your proprietary operating system
I wouldn't buy for a dollar

Offline

#6 2022-05-30 22:21:48

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 552

Re: [SOLVED] Python while loop not working in arch linux

Trilby wrote:
...
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

Board footer

Powered by FluxBB