Python Forum

Full Version: Unable to print stuff from while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Line 13 : print(mytab[i])
If the value of i exceeds 3, you "break".
index [4] is not assigned a value.
Paul
Thank you alot for your help. I now figure out the problem thanks to you. One more time, thanks a lot
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)   
(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