Python Forum

Full Version: help me convert php's for into python's for
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to make a loop from this php's for, but I want to use it on python,
whould you like to help me, because when I try to search it on Google I have no idea what keyword linked to.

Quote:$b=5;
for($a=2;$a<=$b;$a++):
echo $a;
endfor;

I've been trying this python code, but it won't work..

for a in range(2,5):
    print(a)
The upper limit of range should be 6 in order to include 5. Apart from that the code looks fine
(Aug-07-2017, 05:55 AM)buran Wrote: [ -> ]The upper limit of range should be 6 in order to include 5. Apart from that the code looks fine

my python code is not what i want,
because if I use the python script that I've write it's mean that
Quote:I print from 2 until 5 (4)
2
3
4

but what I want is

Quote:I want to print from 2, <=5 times and add 1 per item

2
3
4
5
6
7
Well, that is not what the PHP code does..
(Aug-07-2017, 06:27 AM)buran Wrote: [ -> ]Well, that is not what the PHP code does..

but, the poin is I want this output in python,
please help me..

Quote:$b=5;
for($a=2;$a<=$b;$a++):
echo $a;
endfor;

the output should
Quote:2
3
4
5
Did you try what I suggested in my first post?
for i in range(2,6):
    print(i)
range(start_inclusive, ends_exclusive)
(Aug-07-2017, 06:11 AM)dwiga Wrote: [ -> ]I want to print from 2, <=5 times and add 1 per item
2
3
4
5
6
7

I don't understand that output.  If it's 2<=5, it's (2, 3, 4, 5), which is 4 items.  If you add 1 per item, you end up with 8 items (6, 7, 8, 9).  Why does your output end at 7?
also, there is reference php code...