I want to warn you in advance that I am not an English speaker, but I try to speak more simply so that it is clear.
Relatively recently, I started learning Python and now I need to create code in which the user will need to enter the file name and its extension one by one. You should end up with a list. The length of the list is unknown, so I added the stop word “exit”.
def lst_images():
name = ''
lst = []
while name != 'exit':
name = input('''Enter the names and extensions of the images one at a time.
When you're done, type "exit".''')
if name != 'exit':
lst = lst.insert(-1, name)
else:
break
return lst
user_lst = lst_images()
print(user_lst)
I just tried the “while” loop, it didn’t work, so I came up with this option. I also tried to run the code through the “Debugger” and found out that the problem occurs precisely at the stage of adding an item to the list. After the command lst = lst.insert(-1, name), the list has the value NoneType. I tried adding it in different ways both through .append() and using a counter of the type:
n = 0
while *:
*
lst = lst.insert(n, name)
n = n +1
Nothing helps, I wouldn’t be surprised if the mistake turns out to be banal inattention, but for the life of me I can’t figure out what I’m doing wrong.
I also ask you, if possible, to describe your answer in as much detail as possible, where and why I have an error? If you suggest some solution, describe how it works or attach a link to an article where this or that function will be described.
Let me remind you that I’m just a beginner in this business and I’m just reaching for knowledge.
Thanks in advance to everyone who will take the time to answer my question.
user24719097 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.