Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(off) and (on) sequence
#1
Hello. What I trying to do is a sequence of nodes that will be connected to each other and pass power if they are on.
I tried doing it as if I was doing the battleship game but it kinda did not work.

board = []
for i in range(1):
    board.append([])
    for j in range(10):
        board[i].append([])
        for k in range(10):
            board[i][j].append('-')

for level in board:
    for row in level:
        print(row)
above is the grid i was using.

I want to create a 10x10 grid that will randomly turn each of the outside dots into a 1 or a 0 in a sequence and when one of them turns into a 0 all the other dots should turn into a 0.
It should look like this:
[Image: 1XbT9hW.png]
Anyone who can point me in the right direction, I appreciate it.
Reply
#2
That is a lot of for loops.

This will create a row of 10 dashes.
row = [['-'] * 10]
This will create board with 10 rows
board = [['-'] * 10 for _ in range(10)]
And this will print the board
print('\n'.join(map(str, board)))
I don't understand what you mean by this and your picture:
Quote:I want to create a 10x10 grid that will randomly turn each of the outside dots into a 1 or a 0 in a sequence and when one of them turns into a 0 all the other dots should turn into a 0.
Do you mean something like this where put zeros in row 2 and ones in column 3?
Output:
['-', '-', '-', 1, '-', '-', '-', '-', '-', '-'] ['-', '-', '-', 1, '-', '-', '-', '-', '-', '-'] ['0', '0', '0', 1, '0', '0', '0', '0', '0', '0'] ['-', '-', '-', 1, '-', '-', '-', '-', '-', '-'] ['-', '-', '-', 1, '-', '-', '-', '-', '-', '-'] ['-', '-', '-', 1, '-', '-', '-', '-', '-', '-'] ['-', '-', '-', 1, '-', '-', '-', '-', '-', '-'] ['-', '-', '-', 1, '-', '-', '-', '-', '-', '-'] ['-', '-', '-', 1, '-', '-', '-', '-', '-', '-'] ['-', '-', '-', 1, '-', '-', '-', '-', '-', '-']
Reply
#3
no. I mean something like this
[Image: 3ifjRBw.png]
The space in the middle of the grid can be ignore i just put it so it looks better, the important is the outside (. or -) those are the ones that will change into a 1 or a 0.
Each node(it can be a (.) or a (-) whatever you prefer) connects to the next node and allows the power to flow to that next node. You can make the first one always be 1 so it would pass the energy to the second one and then randomly the program will decide if the next one is a 1 or a 0. if it is a 0 then the program will finish and assign 0 to all other nodes because the power supply was cut off.
Reply
#4
(Dec-05-2022, 05:54 AM)AlexPython Wrote: those are the ones that will change into a 1 or a 0.

What are default values of "those"?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Dec-05-2022, 07:57 AM)perfringo Wrote:
(Dec-05-2022, 05:54 AM)AlexPython Wrote: those are the ones that will change into a 1 or a 0.

What are default values of "those"?

The default value of the first one should be 1 all the others will vary depending on the previous value. For example the first will always be 1 now because the first one is 1 then the second can be a 1 or a 0, let said that the second note was also a 1, the third note will randomly choose for 1 or 0 similar to the second node only because the previous result was a 1 if the previous result was a 0 then all the future nodes will also be a 0 automatically.

5-nodes
result: 5 different results (1-1-1-0-0) - (1-0-0-0-0) - (1-1-0-0-0) - (1-1-1-1-0) - (1-1-1-1-1)
Reply
#6
This will create an iterator that always starts with 1, it will then randomly choose if it's a 0 or 1, once it is a 0 it will always be 0 until a new version is called.
import random


def on_till_randomly_off(count):
    if not count:
        return
    current_count = 0
    while True:
        if current_count == count:
            return
        yield 1
        current_count += 1
        if random.randint(0, 1):
            break
    while True:
        if current_count == count:
            return
        yield 0
        current_count += 1


print("-".join(str(state) for state in on_till_randomly_off(5)))
print("-".join(str(state) for state in on_till_randomly_off(5)))
print("-".join(str(state) for state in on_till_randomly_off(5)))
print("-".join(str(state) for state in on_till_randomly_off(5)))
print("-".join(str(state) for state in on_till_randomly_off(5)))
Output:
1-0-0-0-0 1-1-1-0-0 1-0-0-0-0 1-1-1-1-1 1-0-0-0-0

Shortened version
def on_till_randomly_off2(count):
    state = 1
    for _ in range(count):
        yield state
        if state:
            state = random.randint(0, 1)
AlexPython likes this post
Reply
#7
Thank you buddy.
Reply


Forum Jump:

User Panel Messages

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