The program must remove all parentheses from the text and everything inside the parentheses. When entering "text)" or "text (text)" everything works fine. But there is an error in case I enter "text(". I'm new to Python and I understand that my code is 100% bulky than it should be. Can you please tell me what's wrong with my code?
print("Программа удаляет из текста все круглые скобки "
"и всё, что было внутри этих скобок.")
# Создал функцию parentheses_text_remove.
# Она делает всю работу, а в конце мы просто вызываем её.
def parentheses_text_remove(text):
# Цикл, чтобы находило все скобки в тексте.
while "(" in text:
# Переменная start, находит открывающую скобку.
start = text.index("(")
"""
Следующая строка кода нужна для удаления пробела перед "(".
Слайсим весь текст до start, т.е. до "(" и ищем последний
пробел до start.
"""
start = text[:start].rfind(" ", 0, start)
# Переменная end находит закрывающую скобку.
end = text.index(")", start)
"""
Объединяем всё, что ДО скобок с тем, что ПОСЛЕ.
Всё, что внутри скобок, включая сами скобки,
в новом тексте не участвует.
"""
text = text[:start] + text[end + 1:]
return text
# Добавил всё в цикл, чтобы имитировать какое-никакое общение.
while True:
initial_input = input("Для запуска введите «Старт», "
"для выхода введите «Выход»."
"\n> ").lower().strip()
if initial_input == "старт":
while True:
# Собственно, ввод и вывод текста.
user_input = input("Введите текст: ")
final_output = parentheses_text_remove(user_input)
if user_input == "":
print("Вы ничего не ввели. Попробуйте заново "
"или введите «Выход» для выхода.")
continue
elif user_input in ["выход", "Выход", "ВЫХОД"]:
print("Выход из программы..."
"\n...завершён.")
exit()
print("Результат:", final_output)
elif initial_input == "выход":
print("Выход из программы..."
"\n...завершён.")
quit()
else:
print("Некорректный ввод. Попробуйте заново.")
continue
Error when trying to enter "text(":
Traceback (most recent call last):
File "D:\PycharmProjects\Python\myfile.py", line 38, in <module>
final_output = parentheses_text_remove(user_input)
File "D:\PycharmProjects\Python\myfile.py", line 19, in parentheses_text_remove
end = text.index(")", start)
ValueError: substring not found
How to make the program ignore "text("?
I tried this:
if ")" not in text[start:]:
text = text[:start] + "(" + text[start+1:]
continue
But it doesn't work. In this case, If I enter "text(" the program does nothing. There is no error, I still can enter additional text but when I push Enter button nothing happens.