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,10like that till 50 without using lists
for i in range(2,51):
if i%5开发者_如何学Python==0:
print i
this is giving me 5,10,15,20
Please help me
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.
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)).
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";}
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.
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 syntaxprint 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 ofi
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, andprint i,
will print in the same line
just my 2 cents
for i in range(1,51):
if i%5 == 0:
print i
else:
print i, ",",
(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:
- Add
i
to the bufferoutput
. - If
i
is a multiple of 5, print the bufferoutput
and reset it back to an empty string. (We do this so that we can start the buffer for the next line.) - If
i
is NOT a multiple of 5, add a comma to theoutput
buffer, so that the next number added will be after the comma. - 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.
for i in range(1,51):
if i%5 != 0:
print str(i)+',' , # trailing comma means no \n
else:
print i
this code will give you the required output.
#python
for i in range(1,51):
if i%5 == 0:
print (i)#this will give \n at the end
else:
print (i,end=",")#this will print the numbers with coma at the end
you can put 'end=""
' to print without a space
for space 'end=" "
'
In one statement, you can unpack a generator expression withing print
and utilise the sep
argument. The benefit of this solution is there's no need for explicit if
/ else
conditional logic.
k, n = 5, 50
print(*(','.join(map(str, range(i, i+k))) for i in range(1, n+1, k)), sep='\n')
# 1,2,3,4,5
# 6,7,8,9,10
# 11,12,13,14,15
# 16,17,18,19,20
# 21,22,23,24,25
# 26,27,28,29,30
# 31,32,33,34,35
# 36,37,38,39,40
# 41,42,43,44,45
# 46,47,48,49,50
精彩评论