Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

python 3.x - Find and print vowels from a string using a while loop

Study assignment (using python 3):

For a study assignment I need to write a program that prints the indices of all vowels in a string, preferably using a 'while-loop'. So far I have managed to design a 'for-loop' to get the job done, but I could surely need some help on the 'while-loop'

for-loop solution:

string = input( "Typ in a string: " )
vowels = "a", "e", "i", "o", "u"
indices = ""

for i in string:
    if i in vowels:
        indices += i
print( indices )

while-loop solution:

string = input( "Typ in a string: " )
vowels = "a", "e", "i", "o", "u"
indices = ""

while i < len( string ):
    <code>
    i += 1
print( indices )

Would the use 'index()' or 'find()' work here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try This :

   string = input( "Typ in a string: " )
    vowels = ["a", "e", "i", "o", "u"]




        higher_bound=1
        lower_bound=0

        while lower_bound<higher_bound:
            convert_str=list(string)
            find_vowel=list(set(vowels).intersection(convert_str))
            print("Vowels in {} are {}".format(string,"".join(find_vowel)))

            lower_bound+=1

You can also set higher_bound to len(string) then it will print result as many times as len of string.

Since this is your Study assignment you should look and practice yourself instead of copy paste. Here is additional info for solution :

In mathematics, the intersection A ∩ B of two sets A and B is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements. For explanation of the symbols used in this article, refer to the table of mathematical symbols.

In python :

The syntax of intersection() in Python is:

A.intersection(*other_sets)

A = {2, 3, 5, 4}
B = {2, 5, 100}
C = {2, 3, 8, 9, 10}

print(B.intersection(A))
print(B.intersection(C))
print(A.intersection(C))
print(C.intersection(A, B))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...