Python Forum
help me convert php's for into python's for
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help me convert php's for into python's for
#1
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)
Reply
#2
The upper limit of range should be 6 in order to include 5. Apart from that the code looks fine
Reply
#3
(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
Reply
#4
Well, that is not what the PHP code does..
Reply
#5
(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
Reply
#6
Did you try what I suggested in my first post?
Reply
#7
for i in range(2,6):
    print(i)
range(start_inclusive, ends_exclusive)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
(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?
Reply
#9
also, there is reference php code...
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020