• Register
1 vote


Hello all,

I am a new, college student who is learning python programming. I am thinking to write a program that will input a text file and read all the vowels from it and determines when it comes to the end of the file. So, is it possible to create a while loop that determines when the end of a file has been reached?

Thanks in advance.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

0 votes


The answer is: YES! You can write a code that read vowels from the input text and determines the end of the line.

vowels = {'A','E','I','O','U','a','e','i','o','u'}

with open('my_file.txt') as f:
    vow = sum(ch in vowels for line in f for ch in line.strip())
while True:
    line = f.readline().strip()
    if line == '':

Here, we defined our vowels and checked it into our input, and then checked through a While-loop if it reaches the end of the line or not!

Hence, (‘ ‘) define an empty string or (EOL) and of the line.


13 9 6
94,240 points

Related questions

1 vote
1 answer 918 views
Problem: Hello Pal, I have a query regarding the python programming language. The question is, which method will return an empty string when it has attempted to read beyond the end of a file? Do you have any idea about this? Thanks
asked Mar 16, 2020 Gavin 15.3k points
0 votes
1 answer 43 views
Problem: Hi there! I am confused in while loops and I need to write a program in which a value is taken as input from the user. If the value is less than 0, the program should increment it by 5 as long as the value remains less than 0. As the value becomes greater than or equal to 0, it should print the number of times the value has been incremented and the final value. Any help??
asked Jan 21 Code Learner 9.2k points
1 vote
2 answers 10 views
Problem: How to break a while loop with if condition ?
asked Mar 6 Ashty 6k points
0 votes
1 answer 1 view
1 view
Problem: What is the difference between an if statement and a while loop?
asked 13 hours ago anika11 4.1k points
0 votes
1 answer 2 views
Problem: I have written following code that prints the table of numbers from 1 to 10. ch = 'Y' while ch == 'Y': for i in range(1,10): for j in range(1,10): print(i, "x", j, "=", i*j) print("Tables printed: ",i) When I run this program, it ... exit the while loop and print the number of tables printed on the screen. I do not know what I am doing wrong and why my program keeps running. Please help!
asked Mar 24 Code Learner 9.2k points
0 votes
1 answer 9 views
Problem: How Inner while loop prints a single row after its complete execution? Outer while loop helps to print n number of rows. In other words, outer while loop prints the rows, while inner while loop prints columns in each row.
asked Jan 24 Mashhoodch 13k points
0 votes
1 answer 20 views
Problem: Hi there! I want my while loop to stop after it reaches a certain point and jump out of the loop. Is there any mechanism that I can use? Please help!
asked Oct 12, 2020 Code Learner 9.2k points
0 votes
1 answer 51 views
Problem: Hello Programmers, I am a noob and just came to know about the loop constructs in C#. I tried running while and do-while loops in a program and got different output. Following are the programs that I executed: //Program 1 int a = 0; do { Console.WriteLine( ... ; } Console.ReadKey(); The output has confused me and I want to know the major difference in the while and do-while loops. Thanks!
asked Nov 28, 2020 Code Learner 9.2k points
0 votes
1 answer 39 views
Problem: Hello! I am a beginner, and I have a question. Why does the while() statement in the do-while loop, require a semi-colon after it? Why is this not so in the while loop? I am a bit confused. I am looking forward to a detailed answer and explanation from you. Thanks!
asked Jan 8 Code Learner 9.2k points
1 vote
1 answer 186 views
Problem: I wrote a program that asks the user to enter the amount of a purchase and then it sums up the total bill with 15% vat and 0.07% service tax. I wrote the program in python: vat = 0.15 sales_tax = 0.075 product_price = float(input("Please enter the ... not defined I can not solve this thing. I am new in python programming language so please help me to solve this error. Thanks in advance.
asked May 6, 2020 samhaz 5k points