Python Forum
Unable to print stuff from while loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unable to print stuff from while loop (/thread-29729.html)



Unable to print stuff from while loop - Nick1507 - Sep-17-2020

Alright so hello everyone, I'm a noob and have really little experience in Python so pls go easy on me. My question in this post is why I'm unable to print(mytab[i]) in this code.
i=0

mytab = list(range(1,5))
while True:

    mytab[i]+=mytab[i]
    print(mytab[i])
    i+=1
  
    if i>3:
        break
    
print(mytab[i])     
        
It is just a simple one I think so but i can't detect the problem in this code. So pls can you guys help me in fixing this problem and see what is the issue that i am facing.


RE: Unable to print stuff from while loop - DPaul - Sep-17-2020

Line 13 : print(mytab[i])
If the value of i exceeds 3, you "break".
index [4] is not assigned a value.
Paul


RE: Unable to print stuff from while loop - Nick1507 - Sep-17-2020

Thank you alot for your help. I now figure out the problem thanks to you. One more time, thanks a lot


RE: Unable to print stuff from while loop - cnull - Sep-17-2020

Because it is outside the index element. scan by loop:
i=0
mytab = list(range(1,5))
while True:
    mytab[i]+=mytab[i]
    i+=1  
    if i>3:
        break
for item in mytab:     
    print(item)   



RE: Unable to print stuff from while loop - Nick1507 - Sep-17-2020

(Sep-17-2020, 01:42 PM)DPaul Wrote: Line 13 : print(mytab[i])
If the value of i exceeds 3, you "break".
index [4] is not assigned a value.
Paul
Thanks a lot for your help