+ 1

[Python] Need help in writing list program that removes square brackets as well as contents stored within from userinput

My current program at the moment: filename = [] while True: fileinput = input("Filename? ") if fileinput == "": break else: filename.append(fileinput) print(filename) Key Points: - Must be able to take in any input - Able to remove [] as well as any content stored inside the [] if present, if no [] are present. - Cannot use import re - Preferable to use splicing method Any help will be appreciated as I am new to python and have troubles formulating the condition.

21st Oct 2020, 1:18 PM
Frost Spectre
Frost Spectre - avatar
9 Respuestas
+ 2
If there only one '[]' in your input, then use something like: ind0 = fileinput.find('[') ind1 = fileinput.find(']') + 1 and then slice out these two indices from your string like: fileinput = fileinput[:ind0] + fileinput[ind1:] Ofcourse there are many solutions for this, at least this one exists.
21st Oct 2020, 1:28 PM
QTWizard
+ 2
Ah icic. Thank you for help! Your explanation and code have been really helpful for me. Glad that I learnt something new today.
21st Oct 2020, 3:18 PM
Frost Spectre
Frost Spectre - avatar
0
Any idea how i can merge your code into mine as i need to keep prompting filename unless empty string is given.
21st Oct 2020, 1:43 PM
Frost Spectre
Frost Spectre - avatar
0
filename = [] while True: fileinput = input("Filename? ") if fileinput == "": break else: ignore = 0 for char in fileinput: if char == "[": ignore = 1 elif char == "]": ignore = 0 elif ignore == 0: filename += char filename.append(fileinput) print(filename) Like this? It creates a very weird output though and does not remove []
21st Oct 2020, 2:06 PM
Frost Spectre
Frost Spectre - avatar
0
But am sure that i probably not doing it right. Pls guide me if possible.
21st Oct 2020, 2:06 PM
Frost Spectre
Frost Spectre - avatar
0
Like this? filename = [] while True: fileinput = input("Filename? ") if fileinput == "": break else: ignore = 0 for char in fileinput: if char == "[": ignore = 1 elif char == "]": ignore = 0 elif ignore == 0: filename += char filename.append(filename) print(filename) Input: Filename? test [test] Filename? [test] test Output: ['t', 'e', 's', 't', ' ', [...], ' ', 't', 'e', 's', 't', [...]]
21st Oct 2020, 2:15 PM
Frost Spectre
Frost Spectre - avatar
0
I need the output to be [test, test]
21st Oct 2020, 2:16 PM
Frost Spectre
Frost Spectre - avatar
0
Thank you it works as intended. If it doesn't trouble you, can you please help explain what does the lines mean: else: ignore = 0 fileinput2 = "" for char in fileinput: if char == "[": ignore = 1 elif char == "]": ignore = 0 elif ignore == 0: fileinput2 += char filename.append(fileinput2) Sorry this is the first time i am using ignore command.
21st Oct 2020, 2:35 PM
Frost Spectre
Frost Spectre - avatar
0
So ignore = 1 stops any input from being appended to list till ignore becomes 0?
21st Oct 2020, 3:04 PM
Frost Spectre
Frost Spectre - avatar