views:

269

answers:

8

I have to write a program to print the numbers 1 to 50, but with 5 numbers in a row, like:

1,2,3,4,5
6,7,8,9,10

like that till 50 without using lists

for i in range(2,51):
   if i%5==0:
      print i

this is giving me 5,10,15,20

Please help me

+6  A: 

A few hints:

  • The first parameter for the range() function is incorrect. It must be 1 not 2. The start index included, the ending one is excluded. This is btw a very good practice to avoid bugs.
  • With the modulo operator you found a way of detecting every 5 values; you could use this to print a line when that happens. You then would be left with having to find a way to not output a line when you output one value at at time; look in the python documentation for way to make the print() function do this. (attention, things have changed in the area of print with Python 3.0, but you seem to be using a 2.x version given your example)
  • An alternative approach is to use the 3rd argument of the range() function, to step though the sequence, in increments of 5). Such a range would give you 1, 6, 11, 16 etc. in sequence, and you can use this number as the first of each line, and simple addition for the following ones.
mjv
A: 

I've never used Python, but this should be close if not right.

for i in range(1,51):
      print i;
      print ",";
      if (i%5==0)
         { print "\n";}
waiwai933
Gruh, stop giving away answers to homework questions.
Andrew Song
Don't worry, he used braces.
recursive
ick, you used semicolons too... clearly never used python :-).
Tom
And he's missing the colon after the if statement.
Tim Pietzcker
A: 

Also, remember that using print will cause a new line to start. Unless as noted above a comma is used afterwards.

You can collect values using a string for printing out later.

Jesse
A: 

Some Hints

  • To print 1 to 50, you need to pass n+1 for eg, 1 to 51 in range function, its (i=1;i<51,i++) in C alike syntax

  • print comma if you want between every digits

  • to print line break for every 5, you can just use current if i%5==0: but print blank line, instead of i

  • To concatenate num + string "," you can use `i` or str(i) , you can do like `i` +","

  • If you dont need comma in the end, you could do like i%5 print "," else print "\n" =>

    (i%5 and "," or "\n")

  • print i will print i line by line, and print i, will print in the same line

just my 2 cents

S.Mark
+2  A: 

You can start almost like you did (except you need to start from 1 - that 2 is really weird!-):

for i in range(1,51):
   if i % 5 == 0:
      print i

but then you need to segue into an else clause for that if, because you do want to print something even when i is not a multiple of 5 -- just something different from the simple print i you're already doing when i is a multiple of 5...:

   else:
      print i, ',',

As other answers already said, the trailing comma means "no newline yet"!-) ((It's plainer and more sensible in Python 3.whatever, but you're clearly using Python 2.something, and in those versions this is what you need to do)).

Alex Martelli
and let's not forget... since he "can't use lists"... change range to xrange :-).
Tom
@chrispy, good spotting, let me edit to fix
Alex Martelli
A: 
for i in range(1,51):
    if i%5 == 0:
        print i
    else:
        print i, ",",
inspectorG4dget
A: 
(there used to be python code here)

EDIT: My apologies, didn't realize this was homework. I reeeally need to check tags before answering. Here's an explanation of what needs to happen.

Obviously you know that you're trying to output consecutive numbers from 1 to 50, so you'll need a counter. You've figured out that it'll require a range() call, but it needs to be from 1 to 51, not from 2 to 50.

The reason the range() call needs to be from 1 to 51 is this: it will start the variable i at 1, then check to see if it has reached its goal (51) before looping. If the goal is reached (meaning if i == 51) it will quit the loop without executing the loop's code. So rather than go from 1 to 50, you go from 1 to 51 so that we don't skip the 50th iteration.

Next, you're going to want to have the numbers appear on-screen. But using python's print command prints each number on a new line! That's obviously not what you want. So you're going to have to create a buffer string to append each number to until you're ready to print the line. You can call it 'output' or whatever.

Personally, I like to clear the buffer BEFORE the for loop just to be sure no residual memory traces find their way into the output. Call me paranoid. So I write output = "" on a line before the loop.

Now you've got a buffer string, all you need to do is follow a logical flow:

  1. Add i to the buffer output.
  2. If i is a multiple of 5, print the buffer output and reset it back to an empty string. (We do this so that we can start the buffer for the next line.)
  3. If i is NOT a multiple of 5, add a comma to the output buffer, so that the next number added will be after the comma.
  4. Continue our loop.

These steps should be pretty simple to figure out. Step 2 you've already seen before... To check if a number is a multiple of another number, simply use %. If A % B == 0 then A is a multiple of B.

This should be a pretty straightforward explanation for how to solve this problem. Hope it helps.

And sorry for ruining your learning experience by posting the answer! Now you'll understand why the answer works.

Chris
In fact, not just SHOULD, but DOES.
Chris
I think it might be better to not do his/her homework – gentle pushes in the right direction.
Isaac Hodes
Ah... My apologies, somehow I missed the part where this is a homework assignment. Oops.
Chris
A: 
for i in range(1,51):
    if i%5 != 0:
        print str(i)+',' , # trailing comma means no \n
    else:
        print i
wisty