Python Forum
A "Nested Loop" example from python 3.3.4 to 2.6!!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A "Nested Loop" example from python 3.3.4 to 2.6!!
#1
hi,
 i'm a fresh new user of this forum as well as a complete beginner of the programming world actually. i've done my masters in mathematics 2 yrs back and currently i'm a school teacher. Besides that i'm a computer lover, who loves to try new things for fun and hobby. Last week i was looking at my local newspaper and suddenly a word caught my eyes.... "PYTHON". Honestly at first i thought of it as a snake story but when i started reading that article my respect for that word began to grow. For further research i googled it and then i came to know it is everywhere . Though i'm a computer lover as i said earlier but mostly my love is for high end computer games and accessories , so right that moment something clicked inside me and i thought why not try something innovative as for fun and leisure hobby and i started my hobby of exploring the great world of python by buying my First ever programming book "Beginning Programming with Python For Dummies". Though i don't knew at first as it was a good book or not but i thought it's a start. So, i started with first five chapters of that book and my interest began to grow. so far i've learned:
1.Variable types and declaration. 2.operators. 3.creating and using functions. 4.Decision making statements. 5.Loops.
Though currently i'm studying sixth one "Handling Errors and Exceptions" but while studying 5th one i've come across a topic named "Nested Loops". Though i'm cleared with the idea but the example problem given in the book causing some problems to me. The reason is simple as the current version of python is 3.x.x. But i'm using 2.6 in my laptop and a regular 3.3.4 in my desktop. Now the example given in the book was to Display the multiplication table from 1 to 10.The 3.3.4 equivalent code is given as follows:
X = 1
Y = 1
print ('{:>4}'.format(' '), end= ' ')
for X in range(1, 11):
print('{:>4}'.format(X), end=' ')
print()
for X in range(1,11):
print('{:>4}'.format(X), end=' ')
while Y <= 10:
print('{:>4}'.format(X * Y), end=' ')
Y+=1
print()
Y=1
The output should look like this:

Output:
      1   2   3   4   5   6   7   8   9   10    1  1   2   3   4   5   6   7   8   9   10    2  2   4   6   8   10  12  14  16  18  20      3  3   6   9   12  15  18  21  24  27  30    4  4   8   12  16  20  24  28  32  36  40    5  5  10   15  20  25  30  35  40  45  50      6  6  12   18  24  30  36  42  48  54  60    7  7  14   21  28  35  42  49  56  63  70    8  8  16   24  32  40  48  56  64  72  80    9  9  18   27  36  45  54  63  72  81  90    10 10 20   30  40  50  60  70  80  90  100 
But i'm trying to run this same code on 2.6, as a result system error popped up saying:

"There's an error in your program: invalid syntax".

the error showed me it has problem with the "end" attribute but honestly i didn't find anywhere its 2.6 equivalent. After that i tried to modify the program by myself by changing the print function to print statement and then changed the range() function to xrange() and so on like this:

X = 1
Y = 1

print  '{:>4}'.format(' '), end (' ')

for X in xrange(1, 11):
   print  '{:>4}'.format(X), end(' ')
   
print
   
for X in xrange(1,11):
   print  '{:>4}'.format(X), end(' ')
   while Y <= 10:
      print '{:>4}'.format(X * Y), end(' ')
      Y+=1
   print
   Y=1
i know it is a nonsense and very stupid like thing to do, but as i said i am a complete beginner, the resulting error looked like this:

Error:
Traceback (most recent call last):   File "C:\Users\arun\Desktop\nestedloop2.py", line 4, in <module>     print  '{:>4}'.format(' '), end (' ') ValueError: zero length field name in format
so, now i am here looking for some help.. i am a little hesitated too because i don't know as there any beginners section does exist or not. If someone can tolerate my thread please give some input into this matter and any suggestions regarding beginners forum or something where i can ask many questions regarding this subject  Smile .. Thank you!
Reply
#2
In 2.6 change
print  '{:>4}'.format(' '), end (' ')
to
print  '{:>4}'.format(' '),
for the same effect.
Reply
#3
thanks  admin @Yoriz for your valuable reply first of all. i've tried as you've mentioned :

X = 1
Y = 1

print '{:>4}'.format(' '),

for X in xrange(1, 11):
   print '{:>4}'.format(X),
   
print
   
for X in xrange(1,11):
   print '{:>4}'.format(X), 
   while Y <= 10:
      print '{:>4}'.format(X * Y), 
      Y+=1
   print
   Y=1
But the result was the same as before.

Error:
Traceback (most recent call last):   File "C:\Users\arun\Desktop\nest.py", line 4, in <module>     print '{:>4}'.format(' '), ValueError: zero length field name in format
Reply
#4
Try changing to:
print '{0:>4}'.format(' '),
In python older than 2.7/3.1 field names(0 in this case) are required when calling .format().
Reply
#5
wow! That's work like a charm. thanks a zillion times @stranac . I was actually puzzled because i couldn't find the fault anywhere but not any more  Big Grin .[b][url=http://python-forum.io/User-stranac][/url][/b]

  Now for my second question, may i ask beginner type questions whenever i get stuck? If the answer is yes then i will be highly obliged because this python thing is attracting me more and more and i think that way i can learn programming with ease. Definitely those responses in this thread poses to me a great deal of encouragement and surely i will be looking for more of that. Thanks Again...
Reply
#6
(Dec-18-2016, 02:45 PM)sarada2099 Wrote: Now for my second question, may i ask beginner type questions whenever i get stuck?

Sure, and welcome to python! Smile
Reply
#7
Great! That's the assurance i was needed  Smile  .  Now as both my questions are answered i would like to ask admin to happily mark this as solved...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Big O runtime nested for loop and append yarinsh 4 1,331 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  How do I add another loop to my nested loop greenpine 11 4,443 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Error on nested loop : Invalid syntax dvazquezgu 3 3,177 Nov-25-2020, 10:04 AM
Last Post: palladium
  Nested loop indexing Morte 4 3,808 Aug-04-2020, 07:24 AM
Last Post: Morte
  Nested for loop not looping puttingwordstogether 0 1,672 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  Help: for loop with dictionary and nested lists mart79 1 1,834 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Nested Loop for user input Ads 2 3,525 Dec-30-2019, 11:44 AM
Last Post: Ads
  openpyxl nested for loop help rmrten 3 5,610 Oct-16-2019, 03:11 PM
Last Post: stullis
  nested for loop dilemma YoungGrassHopper 9 4,069 Sep-13-2019, 03:56 AM
Last Post: jsira2003

Forum Jump:

User Panel Messages

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