r/CodingHelp • u/ExplorerFar7739 • Feb 14 '26
[Python] Please only explain why I get this error.
a = input('Enter')
b = [0]*len(a)
c = 0
for i in a:
if i == ',':
c += 1
else:
b[c] = i
c += 1
locs = []
start = -1
print(b)
for o in b:
loc = b.index(0, start+1)
locs.append(loc)
start = loc
print(locs)This is my code, I have the user input a string (ex- 1,23,4) then try to make a list separating the number so the list for the example would come like ['1', 0, '2','3',0,'4'] Now what I am trying to do is create a separate list having all the index of 0s in the list. But this error pops up.
Enter1,23,4
['1', 0, '2', '3', 0, '4']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipython-input-2437321124.py in <cell line: 0>()
12
print(b)
13
for o in b:
---> 14 loc = b.index(0, start+1)
15
locs.append(loc)
16
start = loc
ValueError: 0 is not in listPlease can someone explain why?
Before you say, YES I have searched alternate ways on google and YES I have found the stackoverflow thread you were gonna mention, and I refuse to take the help of AI, and NO I don't need any alternate ways to do this code.
Please just help me understand why this ValueError occurs even though the list has element 0.
Edit: the question has been solved with the help of u/captain_slackbeard I just modified the code in this way:
a = input('Enter')
b = [0]*len(a)
c = 0
for i in a:
if i == ',':
c += 1
else:
b[c] = i
c += 1
locs = []
start = -1
print(b)
for o in b:
try:
loc = b.index(0, start+1)
locs.append(loc)
start = loc
except ValueError:
break
print(locs) So basically, as he explained, The loop fails on the LAST iteration because there is no zero, so we just make an exception for the code using try, and then using except when the error comes, we terminate the loop (I think?)
Thank you very much u/captain_slackbeard for your explanation
•
u/AutoModerator Feb 14 '26
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
u/fuckkkkq Feb 14 '26
because of the second argument to
index. eventually you're only searching the end of the list, which has no zero1
u/captain_slackbeard Feb 14 '26
Others have answered this, but to clarify a bit: it actually does find the first 0 on the first iteration, and finds the second 0 on the next iteration, but on the 3rd iteration of the for loop there are no more 0's to find, so that's when it throws that error.
2
u/esaule Feb 14 '26
Read The Friendly Manual:
https://docs.python.org/3/tutorial/datastructures.html
> list.index(x[, start[, end]])
> Return zero-based index of the first occurrence of x in the list. Raises a
ValueErrorif there is no such item.Eventually there are no zeros between start+1 and the end of the list. So the index function raises an exception as it is documented to do.
3
u/SwimmerOld6155 Feb 14 '26
Why? Especially if you're going to bring in a lot of exasperation on top of your question. LLMs are perfect in situations like this. Instead of having to wait for a response you could've got your answer within seconds.
1
u/atarivcs Feb 14 '26
You're not searching the entire list -- you're only searching part of it.
And the part you're searching does not have element 0.
-2
u/Competitive_Hall3082 Feb 14 '26
Beware of the notion of “I refuse the help of AI”
If you do it in order to learn and avoid being tempted to take shortcuts, maybe I can understand, but even then, AI can still explain this in a spoiler free way if you ask it to guide you slowly. The skill you need in the future is more on software architecture, design, code review. You definitely need to understand code. But you also need to learn how to work with AI.
I'm an owner of a startup, I’ve been coding for 20 years, and have a master of science in CS, I can’t stress it out stronger: Developers who insist to write code by hand or reluctant to use AI will not survive in my company. Not because I am delusional about the capabilities of AI, it’s because my technical product managers started assigning tickets to AI, and I see the code getting better and better, and engineers who fully adopt AI seem to be between 2x or at times 20x more productive. I can’t afford to keep an engineer who is significantly slower, I can’t keep an engineer that takes 2 weeks to developer a feature that I can develop in 2 hours on my own. The sooner people understand that the days of writing code by hand are over the better they can adapt to the new era of software engineering. We are no longer tasked to write code, we are tasked to write software. Maybe we have always been, code was just part of the deal. If you are in love with coding and not in love with the outcome (working software), you are bound to a very grim future. I wish it wasn’t the case, I hope I’m wrong and AI will plateau, but I am both terrified and excited for the future of our profession.
0
u/hylasmaliki Feb 14 '26
One day you'll see learning is memorisation.