Posts: 98
Threads: 43
Joined: Dec 2016
Jun-20-2019, 05:58 AM
(This post was last modified: Jun-20-2019, 05:25 PM by mcmxl22.)
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')
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 98
Threads: 43
Joined: Dec 2016
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.
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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
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.
Posts: 360
Threads: 5
Joined: Jun 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')
Posts: 2,128
Threads: 11
Joined: May 2017
Jun-20-2019, 09:29 AM
(This post was last modified: Jun-20-2019, 09:29 AM by DeaD_EyE.)
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.
Posts: 3,458
Threads: 101
Joined: Sep 2016
It seems like some of ya'll are missing the point. This is emulating the feel of the monitors from the matrix:
Posts: 360
Threads: 5
Joined: Jun 2019
please show us your code for this :-)
|