+ 4
Why i am getting an error ?? Need help pls
I am getting the result including the following error message # Error message <module> item = next(nums_iter) StopIteration # Here is the code nums = [3, 4, 5, 8, 10] nums_iter = iter(nums) while True: item = next(nums_iter) print(item)
7 Answers
+ 3
Hi...
StopIteration is an exception which is raised by next() method to tell that there is no more values present.
There can be two solution for this :
#using try catch
nums = [3, 4, 5, 8, 10]
nums_iter = iter(nums)
try:
while True:
item=next(nums_iter)
print(item)
except StopIteration:
pass
#adding dummy item at last
nums = [3, 4, 5, 8, 10]
nums_iter = iter(nums)
while True:
item=next(nums_iter,'end')
if item=='end':
break
else:
print(item)
0
Take code and try it in code window in sololearn and you will see it runs.
I don't understood why you dislike my answer.
When a code don't run, I tell that and I purpose a solution if I can.
0
attn: MBZH31
that's not how you help somebody just typing "No error". The way Rishi Anand helped me that's the way you do otherwise don't attempt. And by the way i don't know how you compile and run the code. The code does cause ar error.
0
🄼🄼🄷🄺
I copy and paste your code in sololearn application in the code window.and i run it.
That's all
Look and test
https://code.sololearn.com/cWDjEq18SB42/?ref=app
0
Just let it go, please i can understand you been trying to prove there is no error. I clicked your link with my code and still same error. so you better check yourself
- 2
No error
- 4
Stop being rude MMHK