I
I
Ipm_tg_07hash2021-11-21 13:41:18
Python
Ipm_tg_07hash, 2021-11-21 13:41:18

IndexError: string index out of range - how to fix?

I tried to make a program that converts text from Cyrillic to Latin. Moreover, the letter combination "kv" must be converted to "qu".
"kv", of course, is converted to "qu", but if you enter just "k" instead of "kv", then instead of "k" the program gives an error "IndexError: string index out of range" on line 6: if q == "к" and qw[i+1] == "в":
Please help. I am attaching the code below:

qwerty = {"к": "k", "в": "v", "а": "a"}
qw = input("введи букву/слово")
i = 0
q = qw[i]
while i<len(qw):
    if q == "к" and qw[i+1] == "в":
        print("qu")
        i += 2
    else:
        q = qw[i]
        print(qwerty.get(q))
        i += 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jerwright, 2021-11-21
@jerwright

Modify your condition in which the error occurs, as follows:

if q == "к" and and len(qw)>1 and qw[i+1] == "в":
  #ваш код ниже

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question