Python Forum
for x to y step from basic in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: for x to y step from basic in Python (/thread-29272.html)



for x to y step from basic in Python - lastyle - Aug-25-2020

Hi all,

i previously asked for a Solution for a routine which compares two values and also does a step. It probably was explained to complicated to understand, so i coded the working Solution in Basic for "easier" understanding

How would this be done in Python ? Thanks in advance

[basic]
a=3940
b=5630
x=200
y=5
arr=""


looproutine
for i as integer = y to x step y
arr=arr+"s"+str(a)
a=a+y
next i
if a < b + y then
arr=""
goto looproutine
else
alldone

alldone
exit
[/basic]


RE: for x to y step from basic in Python - Gribouillis - Aug-25-2020

It is not easier in Basic. Can you explain what the code does in plain english?


RE: for x to y step from basic in Python - jefsummers - Aug-25-2020

first_value = 3940
second_value = 5630
last_in_loop = 200
start = 5
build_string = ""

while True:
    for count in range(start, last_in_loop, start) :
        build_string = build_string + 's' + str(first_value)
        first_value += start

    if first_value < second_value + start :
        build_string = ""
    else :
        break
Note that variables named a, b, x, etc are frowned upon as being nondescriptive. Much of the code is similar to Basic, the for loop is an exception, and a big one is that there is no "goto", true of most modern languages (hence the use of the
while
, which you exit with
break
.


RE: for x to y step from basic in Python - lastyle - Aug-25-2020

Thanks a lot, thats EXACTLY doing what i was looking for. I didnt understand the Range Startement and building.

Now with that Solution i finally got the Idea how it`s working.

CHEEERS!!!