Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Why is my list index out of range in this for loop? [closed]

$
0
0

I've been tasked to write a program that receives user input and then outputs the input but without any vowels.

I have this code:

text = str(input("Input: ")).strip()# find positions of vowelsvow_pos = []vowels = ["A","a","E","e","I","i","O","o","U","u"]k = 0for i in range(len(text)):    for k in range(len(vowels)):        if text[i].find(vowels[k]) != -1:            vow_pos.append(i)        k = k + 1# delete vowels based on positionfor j in range(len(vow_pos)):    if j > 0:        vow_pos[j] = vow_pos[j] - j        text = text.replace(text[vow_pos[j]],"") # <-(Line giving error)    else:        text = text.replace(text[vow_pos[j]],"")print("Output: " + str(text))

But I get an error at the marked line which says IndexError: list index out of range.

Initially, I didn't take into account that upon one character being deleted, the length of the overall string would then also decrease by 1. However, I'm trying to account for that with the vow_pos[j] = vow_pos[j] - j code. It still seems to give an error after 5 or so vowels.

What is wrong, and how do I fix it?


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>