Python Forum

Full Version: Automation in calculations
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear members,

Kindly I would like to inform you that I am working on a little project to automate a calculation process. The idea is that the program produces every second a value based on the previous value. An example would be 2, 4, 8, 16, 32 when a multiplication function is selected. I used a fictional line where steps can be made on.

The following code works with python 3.8 in the sense that it produces +1 values every second and starts with the selected value. However, I could not find on the internet how I might change the code so that it does what I would like it to do: 1) being able to select the step size (which should be a function) and 2) that the program ends with the selected value.

Does anyone know how to solve this issue or where I might forward my question?

import time

start = 3
step = 5
end = 20
wait = 1 #waiting in seconds
for i in range(end):
    i = i + step
    print(i)
    time.sleep(wait)
1) If you "print(i)" you print everything from 0 to "end-1"
2) If you want to start with "start" you'll need to increment using the "+=" operator
3) It's not entirely clear what you mean by "selected value", is it "end"?

How will you make sure that if you increment "start" with "step" that it will come out
exacly on "end". (Or bigger than ?)...

(Unless you are looking to implement the parameters in the for loop (start, end, step),
then you'll need another approach.)

Paul
You are mixing up while loops and for loops. Notice that the for loop does incrementing itself.
import time
 
start = 3
step = 5
end = 20
wait = 1 #waiting in seconds
i = start
while i < end:
    print(i)
    time.sleep(wait)
    i = i + step

for i in range(start, end, step):
    print(i)
    time.sleep(wait)
(Dec-10-2020, 03:43 PM)DPaul Wrote: [ -> ]1) If you "print(i)" you print everything from 0 to "end-1"
2) If you want to start with "start" you'll need to increment using the "+=" operator
3) It's not entirely clear what you mean by "selected value", is it "end"?

How will you make sure that if you increment "start" with "step" that it will come out
exacly on "end". (Or bigger than ?)...

(Unless you are looking to implement the parameters in the for loop (start, end, step),
then you'll need another approach.)

Paul

Dear DPaul,
Thank you for your kind reply. 3) Yes, the value stated for "end".
(Dec-10-2020, 03:57 PM)deanhystad Wrote: [ -> ]You are mixing up while loops and for loops. Notice that the for loop does incrementing itself.
import time
 
start = 3
step = 5
end = 20
wait = 1 #waiting in seconds
i = start
while i < end:
    print(i)
    time.sleep(wait)
    i = i + step

for i in range(start, end, step):
    print(i)
    time.sleep(wait)

Dear deanhystad, Thank you for sharing some example code. The 'while loop' works great. I have was also able to adapt to produce a linear, exponential, and others functions. I would say: case closed and thank you very much. Here is a little example:

import time

start = 2
step = 0
end = 8000
wait = 0.5  # waiting in seconds
i = start
while i < end:
    print(i)
    time.sleep(wait)
    i = i ** 2 + step # with one '*' the values 2, 4, 8, 16, 32 etc.
You could make a generic loop where you pass in how the increment is done.
import time
import operator

def loop(func, start, stop, step=1, incr=operator.add):
    while start < stop:
        func(start)
        start = incr(start, step)

def print_and_wait(value, wait=0.0):
    print(value)
    time.sleep(wait)

loop(lambda count=0: print_and_wait(count, 0.5), 1, 100, 2, operator.mul)
Probably no all that useful, but it does demonstrate that while had a lot more flexibility than for.