Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Big Dave

macrumors 6502
Original poster
Nov 27, 2007
314
25
Crestview, Fl
While working at home, I thought I would try out python. I'm running 2.7.
I usually write in bash and one of the BASIC programs The Woz demo'd on the Apple 1 was: (converted to BASH)
Bash:
for i in {0..10}; do for ((j=0; j<=$i; j++)) do echo -n $j; done; echo ''; done;
This would provide:
0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
012345678910

While trying to write this same code in Python, I do not get the desired result and I was wondering if anyone had any advice.
Python:
#!/usr/bin/python
import sys
# Define a main() function
def main():
  for i in range(0,10):
    for j in range(0,i):
      print j,
      sys.stdout.write('')
    print
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
  main()
This provides:

0
01
012
0123
01234
012345
0123456
01234567
012345678

My questions are: 1. Why is the first line blank? 2. Why does the loop only go through 8? I notice there are 10 lines of output, but it's not what I'm looking for.

PS.. this is not homework.
 
In your bash code you use j<=$i.
In your Python the range probably goes j<i.

I have never written any Python, but it looks like that'd explain the behaviour.

So I goes 0 through 9, but since the 0th round is also executed, that's 10 runs, but the j loop doesn't do anything when I=0 cause 0!<0 and when I=9, the largest I gets to, j's max will be 8
 
  • Like
Reactions: Big Dave
In your bash code you use j<=$i.
In your Python the range probably goes j<i.

I have never written any Python, but it looks like that'd explain the behaviour.

So I goes 0 through 9, but since the 0th round is also executed, that's 10 runs, but the j loop doesn't do anything when I=0 cause 0!<0 and when I=9, the largest I gets to, j's max will be 8
That makes sense. Ok so now I need to learn range better to do a <=.
 
I figured it out!
@casperes1996 Thank you for your help!
Python:
#!/usr/bin/python
import sys
# Define a main() function
def main():
  i = 0
  while i <= 10:
    for j in range(0, i+1):
      print j,
      sys.stdout.write('')
    i = i + 1
    print
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
  main()
 
I figured it out!
@casperes1996 Thank you for your help!
Python:
#!/usr/bin/python
import sys
# Define a main() function
def main():
  i = 0
  while i <= 10:
    for j in range(0, i+1):
      print j,
      sys.stdout.write('')
    i = i + 1
    print
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
  main()

Congratulations :).

I do think it’d be more elegant to still use a for loop for both cases though. You can still use a for loop for the i variable and just make it go 0,11 or as I said earlier, 1,12.
Or make them both while loops. For is likely faster, but having consistency would be worth something in a larger project I’d say. - For loops are generally a bit easier to reason about, since you don’t have to keep track of the variable in the same way. In for i in range(x,y), it‘s sort of fixed from the start what values it will take. In while i <= 10, you only know once you reach the end of one loop iteration what happens to i along the way.

But that’s an aside, congratulations on getting it figured out. And for learning I guess it’s only positive to have tried both loops
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.