Python Forum
Unable to print stuff from while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to print stuff from while loop
#1
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.
Reply
#2
Line 13 : print(mytab[i])
If the value of i exceeds 3, you "break".
index [4] is not assigned a value.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Thank you alot for your help. I now figure out the problem thanks to you. One more time, thanks a lot
Reply
#4
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)   
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to Loop Over Date gdbengo 2 1,268 Jan-09-2022, 05:11 PM
Last Post: menator01
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore
  Run the code for some stuff it does not return me why Anldra12 3 2,798 Apr-19-2021, 02:01 PM
Last Post: Anldra12
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,467 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Print output in single file using pramika loop deepakkhw 1 2,037 Jul-11-2020, 11:57 AM
Last Post: j.crater
  Unable to combine print statements in for loop adeana 2 1,954 Jun-12-2020, 05:08 PM
Last Post: adeana
  Create, assign and print variables in loop steven_tr 10 4,276 May-28-2020, 04:26 PM
Last Post: ndc85430
  How Do I Install Stuff for Python? CopBlaster 6 3,133 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Learning to have Class and doing stuff with it... bako 2 1,958 Apr-29-2020, 05:07 PM
Last Post: bako
  Print different positions in loop from functions konsular 5 2,652 Oct-16-2019, 08:10 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020