7 Replies - 1018 Views - Last Post: 06 April 2016 - 01:10 PM Rate Topic: -----

#1 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 449
  • Joined: 15-December 14

Trying to make three loops run at the same time

Posted 05 April 2016 - 07:06 PM

Trying to challenge myself and right now I'm working on a project where I can make three loops run at the same time ( I plan to add more later and then try letters). My idea is to make it scroll through the numbers like the way the airports do when they show the arrivals and departures. I made it work using just functions (top example), but I wanted to see if I could make the numbers scroll at different speeds. The top program is my first attempt and below that is what I've been working on. So far when I tried to use inheritance to make each loop run differently I get only one loop to work and it always shows the same number each time it loops.I looked online and couldn't find an example of what I am trying to do.

import random
import time

def foo():
        for i in range(20):
                x=random.randint(1,9)
                y=random.randint(1,9)
                z=random.randint(1,9)
                print x,y,z


foo()




This is my attempt to do it using oop. I racked my brains and I can't figure out what I am doing wrong.
class A(object):
	def a(self):
		x=random.randint(1,9)
		for i in range(20):
			print x
			time.sleep(.1)

class B(object):
	def b(self):
		y=random.randint(1,9)
		for i in range(20):
			print y
			time.sleep(.5)

class C(A,B)/>:
	def c(self):
		z=random.randint(1,9)
		for i in range(20):
			print z
			time.sleep(.1)
		super(A,self).a()
		super(B,self).b()

c=C()
c.c()


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to make three loops run at the same time

#2 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 395
  • View blog
  • Posts: 1,265
  • Joined: 27-December 13

Re: Trying to make three loops run at the same time

Posted 06 April 2016 - 12:35 AM

Just a short answer now - will look more into this later.
I would use the time module to check for actual time differences.
The code provided here is very rough, but demonstrates how the value of val_a and val_b roll on different time differences.
import time, random

# defining the wait times
wait_a = 2
wait_b = 0.7

# initialization
start = time.clock()

start_a = start
start_b = start

val_a = random.randint(1, 9)
val_b = random.randint(1, 9)


while True:
    now = time.clock()
    if now - start_a > wait_a:
        start_a = now
        val_a = random.randint(1, 9)
    if now - start_b > wait_b:
        start_b = now
        val_b = random.randint(1, 9)

    print(val_a, val_b )  # rough print to console


This post has been edited by DK3250: 06 April 2016 - 12:36 AM

Was This Post Helpful? 1
  • +
  • -

#3 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 395
  • View blog
  • Posts: 1,265
  • Joined: 27-December 13

Re: Trying to make three loops run at the same time

Posted 06 April 2016 - 02:23 AM

Let me bother you again; I couldn't resist tuning a little more on this code.
I ended up with a generator function; - it is first time (for me) that the same generator is called more than once - and even with a different parameter in the calls.
I think this is quite interesting.

import time, random

wait_a = 2
wait_b = 0.7
wait_p = 0.1  # defines time intervals for print statements

start = time.clock()
start_p = start

def roll(wait):
    start = time.clock()
    val = random.randint(1, 9)
    while True:
        if now - start > wait:
            start = now
            val = random.randint(1, 9)
        yield val

gen_a = roll(wait_a)
gen_b = roll(wait_b )

while True:
    now = time.clock()
    val_a = next(gen_a)
    val_b = next(gen_b )
    if now - start_p > wait_p:
        start_p = now
        print('% 6.2f %1d %1d' % (now-start, val_a, val_b ))
I hope you will enjoy this.
Was This Post Helpful? 1
  • +
  • -

#4 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 395
  • View blog
  • Posts: 1,265
  • Joined: 27-December 13

Re: Trying to make three loops run at the same time

Posted 06 April 2016 - 08:41 AM

Ok, one more code sample, producing the same output; now in a more traditiondal OOP style:
import time, random

wait_a = 2
wait_b = 0.7
wait_p = 0.1  # defines time intervals for print statements

start = time.clock()
start_p = start

class Roll():
    def __init__(self, wait):
        self.wait = wait
        self.start = time.clock()
        self.val = random.randint(1, 9)

    def roll(self):
        if now - self.start > self.wait:
            self.start = now
            self.val = random.randint(1, 9)
        return self.val

# create the instances
a = Roll(wait_a)
b = Roll(wait_b )

while True:
    now = time.clock()
    val_a = a.roll()
    val_b = b.roll()
    if now - start_p > wait_p:
        start_p = now
        print('% 6.2f %1d %1d' % (now-start, val_a, val_b ))


Was This Post Helpful? 1
  • +
  • -

#5 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 449
  • Joined: 15-December 14

Re: Trying to make three loops run at the same time

Posted 06 April 2016 - 11:44 AM

Not exactly what I wanted. But you gave me a few ideas. What this is the result of my original program using just functions.

7 5 2
8 7 2
3 4 9
6 5 2
1 5 1
4 4 3
1 2 8
1 3 5
4 5 1
5 9 4
7 9 5
9 8 6
6 9 8
5 8 1
2 4 4
1 8 2
4 7 4
2 2 8
2 9 6
8 6 2
a b c


I am trying to make each row loop through numbers individually and each row will run at different speeds. so for example, I'd make the numbers above the letter 'a' run at a different speed and then make each row run slower than the row before it.
Was This Post Helpful? 0
  • +
  • -

#6 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 395
  • View blog
  • Posts: 1,265
  • Joined: 27-December 13

Re: Trying to make three loops run at the same time

Posted 06 April 2016 - 12:07 PM

As long as you print to the console, you are limited to printing the lines in sequence.
I don't think you can run the vertical rows independently.
Do you have Pygame installed? Then it will be possible to control graphics in detail.

For inspiration only (from another project): http://www.mediafire...atrix_video.mp4

This post has been edited by DK3250: 06 April 2016 - 12:12 PM

Was This Post Helpful? 1
  • +
  • -

#7 albert003  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 449
  • Joined: 15-December 14

Re: Trying to make three loops run at the same time

Posted 06 April 2016 - 01:01 PM

I thought about using pygame for the program but I thought I'd try it first just using python. The video is exactly what I wanted to do, minus the coloured back ground and making it look like the movie The Matrix. I wanted a bunch of numbers or letters to scroll down and at the end of the loop spell out a word. Looks like I'll have to try to make it work on pygame. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#8 DK3250  Icon User is online

  • Pythonian
  • member icon

Reputation: 395
  • View blog
  • Posts: 1,265
  • Joined: 27-December 13

Re: Trying to make three loops run at the same time

Posted 06 April 2016 - 01:10 PM

OK, give it a try.
If you get stuck, write here on DIC (possibly in a new tread).
Happy Coding.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1