Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The Matrix
#1
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')
Reply
#2
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]
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
#3
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.
Reply
#4
(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']
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
(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')
Reply
#6
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
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]
Reply
#8
please show us your code for this :-)
Reply


Forum Jump:

User Panel Messages

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