Bottom Page

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!
Quote
#2
In 2.6 change
print  '{:>4}'.format(' '), end (' ')
to
print  '{:>4}'.format(' '),
for the same effect.
Quote
#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
Quote
#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().
Quote
#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...
Quote
#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
Quote
#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...
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  openpyxl nested for loop help rmrten 3 214 Oct-16-2019, 03:11 PM
Last Post: stullis
  nested for loop dilemma YoungGrassHopper 9 343 Sep-13-2019, 03:56 AM
Last Post: jsira2003
  nested for loop dilemma 2 YoungGrassHopper 12 401 Sep-12-2019, 02:06 PM
Last Post: YoungGrassHopper
  Nested while loop problem + turtle DreamingInsanity 3 356 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  Index error using pop in nested loop PerksPlus 3 466 Mar-28-2019, 03:11 PM
Last Post: ichabod801
  Nested for loop strange problem mcva 2 404 Mar-16-2019, 12:53 PM
Last Post: mcva
  Nested while loop in pyramid program. raj55 2 1,915 May-04-2018, 10:05 AM
Last Post: ThiefOfTime
  Nested Loop Problem GSWes 5 1,324 Nov-12-2017, 10:22 PM
Last Post: Larz60+
  Using nested for loop with a single list mikeavison 3 1,024 Aug-12-2017, 08:13 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)