Python Forum
The Matrix - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: The Matrix (/thread-19248.html)



The Matrix - mcmxl22 - Jun-20-2019

The Matrix simulator.

import random
 
characters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
              'A', 'Z', '=', '+', '#', '.', '<', ':', '&', '$']
 
while True:
    matrix = random.choice(characters)
    for i in matrix:
        print(i, end='\t')



RE: The Matrix - perfringo - Jun-20-2019

I am not sure what this code is supposed to accomplish but one suggestion anyway. Don't use Python as typing machine. Let Python do the heavy lifting. You don't need variables just for using in list. There is list comprehension for that. Instead of tedious typing of variables and putting them into list manually one can have list this way:

>>> [num for num in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



RE: The Matrix - mcmxl22 - Jun-20-2019

perfringo, I'm relatively new to Python and programming in general so I don't know all the tricks yet. Thanks for the suggestion but it returns a list of integers and I need a list of strings.


RE: The Matrix - perfringo - Jun-20-2019

(Jun-20-2019, 07:10 AM)mcmxl22 Wrote: perfringo, I'm relatively new to Python and programming in general so I don't know all the tricks yet. Thanks for the suggestion but it returns a list of integers and I need a list of strings.

Thats why I made suggestion Smile

Why do you need list of strings? There is no difference if printing out:

>>> print(1)
1
>>> print('1')
1
However, if you really need strings then slight modification of list comprehension provided can help:

>>> [str(num) for num in range(10)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']



RE: The Matrix - ThomasL - Jun-20-2019

(Jun-20-2019, 05:58 AM)mcmxl22 Wrote:
import random
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
while True:
    matrix = random.choice(num)
    for i in matrix:
        print(i, end='\t')

What is the for-loop for?
matrix is always one element from the list "num", iterating over one element makes no sense.
Simpler:
import random
while True:
    print(str(random.randint(0,9)), end='\t')



RE: The Matrix - DeaD_EyE - Jun-20-2019

The print function handles objects automatically.
If __str__ is present, it will take the string representation.
if __str__ is missing, __repr__ will used as string representation.

import random
while True:
    print(random.randint(0,9), end='\t')
If you use formatting, you can be explicit.
import datetime


f'{datetime.datetime.now()}' # implicit use of __str__()
f'{datetime.datetime.now()!s}' # explicit use of __str__()
f'{datetime.datetime.now()!r}' # explicit use of __repr__()

By the way, curses is very interesting for terminals on Linux.


RE: The Matrix - nilamo - Jun-20-2019

It seems like some of ya'll are missing the point. This is emulating the feel of the monitors from the matrix:
[Image: giphy.gif]


RE: The Matrix - ThomasL - Jun-21-2019

please show us your code for this :-)