• Register
1 vote
240 views

Problem:

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

Solution:

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 == '':
        break

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.

Thanks.

13 9 6
94,240 points

Related questions

1 vote
1 answer 675 views
675 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
1 vote
1 answer 94 views
94 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
0 votes
1 answer 17 views
17 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 5.4k points
0 votes
1 answer 13 views
13 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 5.4k points
0 votes
1 answer 11 views
11 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 5.4k points
0 votes
1 answer 5 views
5 views
Problem: I am confused about the do-while loop. My question is simple. In a do-while loop, which block executes first? Why? Does the while always execute?
asked 1 day ago Code Learner 5.4k points
1 vote
1 answer 15 views
15 views
Problem: I'm working on a simple python program that creates a circle "button" and then having the user click within the circle. If they don't click on the circle, a message pop-up stating that they have clicked outside of the circle area and should give a try again. However, in the ... x-100)**2) +((y-250)**2)) if length == rad or length < rad: break print("Please click within the spin button")
asked Jun 1, 2020 adamSw 11.3k points
0 votes
1 answer 442 views
442 views
The code below is based on code.activestate.com/recipes/413614/ However, the key point of the recipe - that it provides a way to break out of the iteration on an iterator if the iterator is empty - doesn't seem to work here, instead behaving ... next_choice = choice_iterator.next() except StopIteration: print "No matches. Exiting..." sys.exit() else: choice_iterator = itertools.chain([next_choi
asked Oct 28, 2020 psandprop 2.4k points
1 vote
1 answer 76 views
76 views
Problem: Hello people I am doing python programming for a few days. I am still learning. Could anybody please help me by showing how can I import a text file and read five lines from it by my python program? If possible make it as easy words as possible for me. Thanks.
asked Mar 11, 2020 Gavin 15.3k points
0 votes
2 answers 107 views