Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automation in calculations
#1
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)
Reply
#2
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
Reply
#3
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)
interdisciplinary likes this post
Reply
#4
(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".
Reply
#5
(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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Parameterized math calculations? babaliaris 7 16,920 Jun-01-2022, 10:19 AM
Last Post: Gribouillis
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,604 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
Photo Filtering data and precision during calculations Scientifix 0 1,787 Mar-30-2021, 01:00 PM
Last Post: Scientifix
  List calculations rturus 1 1,805 Mar-23-2021, 04:40 PM
Last Post: deanhystad
  Latitude&Longitude Calculations gdimit 3 3,796 Jan-30-2018, 07:47 AM
Last Post: buran

Forum Jump:

User Panel Messages

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