Posts by Woss • 73,416 points
1,476 posts
-
5
votes2
answers3332
viewsA: Preload, Prefetch and Preconnect, what are they for?
Are three possible, among many others, for the attribute rel of the element link. Preload <link rel="preload" href="style.css" as="style"> Enter the value preload makes the client (browser)…
-
1
votes1
answer233
viewsA: Manipulating Htmlcollection elements
Of course, all you have to do is iterate over the result and add the desired class. See a simple example where I return the result with the for: const itens =…
javascriptanswered Woss 73,416 -
2
votes1
answer52
viewsA: Run Javascript within meta tag parameter
As I said, just assign one id for your tag and use the method setAttribute to set the new value. See the example below, where you assign "new value" to the attribute content of tag: const ogImage =…
-
0
votes1
answer67
viewsA: Pick string by format
Like commented, you will have only two formats allowed: "0000 0000 0000 0000 00/0000" and "0000000000000000 00/0000 000", that is, a 16-digit sequence, which can be grouped every four, followed by a…
-
3
votes1
answer1870
viewsA: How to save the result to a file?
The function print has an argument called file, that defines the destination of the message. By default, the argument value is sys.stdout, which is responsible for displaying the messages on the…
python-3.xanswered Woss 73,416 -
12
votes3
answers6001
viewsA: Regex returns False when validating card
If using the class RegExp you do not need to use the bars; these only serve to define a direct regular expression without using the class. If by removing the bars your expression matches other…
-
1
votes1
answer188
viewsA: As for a default color in input color
Just set the attribute value of your field: <input type='color' value='#FFFFFF' /> The value you enter in the attribute will be the color displayed initially.…
-
7
votes2
answers5568
viewsA: How to interpolate string in Python?
For versions prior to 3.6, always use the method str.format and I explain why in this question: What to use to format a string, %, or format? Already, from version 3.6, a new way to perform the…
-
2
votes1
answer996
viewsA: Convert an entire number to a float with decimals
This is nothing more than a peculiarity of Java. When you display a value on the screen, implicitly it is called the method toString of the object. In documentation of this method there is the…
-
5
votes3
answers447
viewsA: Comparison operator or isset()?
The function¹ isset() and the comparison of values are different things, they do different things and their use is not exclusive to each other. The isset() checks if a variable is defined in the…
-
1
votes2
answers343
viewsA: classname, Setattribute or classList.add
The setAttribute should never be used to add a class to an element, for the simple fact that the new value will override the current one and you have no control over which classes the element has at…
-
2
votes1
answer59
viewsA: Why is the class attribute not modified?
If this question is an extension of the previous one, about inter-process communication, you should have followed the tips you were given in it. As I said, the variables are not shared between…
-
1
votes1
answer33
viewsA: Get variable declared in function outside of developer
In Python, explicit is better than implicit. The way you’re doing it would be hiding rules inside the decorator and it would affect the readability of the code. Because having a function that reads…
-
0
votes1
answer252
viewsA: Type error: not all strings converted during formatting
Your error is in using the same object, soma, to store both the result of the sum of numbers and the letter identifying the result. Not only is it a bad approach because it doesn’t make the code…
-
4
votes1
answer36794
viewsA: How do I place items on a list in ascending or descending order?
Python is a dynamic language, meaning that there is no limitation of the type a function can take as argument. The function can receive a string, now can receive an integer and, with this, it is…
-
1
votes3
answers4541
viewsA: Syntax error in Python code. What is it?
There are errors of syntax and logic. Syntax errors are easy to fix: the interpreter shows an error message pointing exactly to the location and error. Traceback (most recent call last): File…
-
6
votes2
answers1295
viewsA: Count certain characters in a word in python
Just display the formatted value using the e. >>> print('{:e}'.format(20000000)) 2.000000e+07 If you want to limit the number of decimals as well, you can do it along with formatting:…
-
14
votes1
answer697
views -
3
votes1
answer916
viewsA: Ordering a list of strings in python
As the question has not yet been perfectly clear - and apparently the author himself has not been able to explain - I will consider this answer strings independent of the format with one condition…
-
1
votes3
answers510
viewsA: How to make an error message for this situation?
As you are creating a function to do the search, it is not interesting that you do the print. This generates side effects: it will not always be desired to run the function and get the result in the…
-
13
votes1
answer2259
viewsA: What to use to format a string, % or format?
For 3.6+ versions, use f-strings for interpolation: How to interpolate string in Python? For versions 3+, prior to 3.6, always use the method format, it was set precisely to replace the % - for…
-
2
votes1
answer56
viewsA: Explode not working for sure?
Within your for, you are comparing an entire value, $i, with a array, $count, and PHP thinks that making this comparison is something quite normal and does not complain about it, but if you choose…
-
4
votes1
answer168
viewsA: Python constructor with **kwargs
The best way is to explicitly state the arguments. Citing the Zen of Python: Explicit is Better than implicit In this case, if you want to leave the parameters as optional, just initialize them with…
-
11
votes2
answers1588
viewsA: Data structure representing a deck of cards
One option is to use collections.namedtuples: from collections import namedtuple from itertools import product Carta = namedtuple('Carta', ['face', 'naipe']) faces = {'A', '2', '3', '4', '5', '6',…
-
1
votes1
answer48
viewsA: Assigning values to a numeric array of values a PHP query
If I understand correctly, you can do something like: if ($result = mysqli_query('SELECT marca FROM marcas')) { $arr = array('1', '2', '3', '4'); $bancoDeDados = mysqli_fetch_all($result);…
-
1
votes1
answer20
viewsA: How to declare abstract methods in OOP interface
When you use an interface, an analogy you can make is that the class becomes a black box and the outside world just sees the interface. That is, the interface will define how the outside world will…
-
0
votes2
answers723
viewsA: How to post without reloading the page?
In addition to the William, we can also request with pure Javascript through the API fetch. At the moment, it is still an experimental tool, but already has support in the main browsers and has a…
-
1
votes1
answer243
viewsA: Save object returned by get method
The return of function $.get is a promise, so you can use the method done whenever you want to use the return obtained. For example, below I did that as soon as the page loads the list of tweets is…
-
1
votes1
answer481
viewsA: Read and change GPIO pin status of Raspberry Pi 3
We can imagine two PHP files: read.php, which will read the current pin state, returning the value in JSON, and write.php, that will change the status of the pin. read php. We can imagine that the…
-
3
votes2
answers73
viewsA: Generating id in input through foreach
You can use the image’s own position on array: foreach($array as $i => $valores) { echo "<a href='#' id='imagem_{$i}'><img src='Admin/Pagina/Produtos/uploads/{$valores}'/></a>";…
-
5
votes2
answers991
viewsA: How to read numbers separated by hyphens in a text file?
If there is a possibility that the file has multiple lines, you can create the following generator: def read(filename): with open(filename) as stream: for line in stream: yield map(int,…
-
2
votes2
answers26379
viewsA: Returning only the highest value of a Python list
Has. Use the function itself max. The problem why your code didn’t work is because you have a list of strings and you want to evaluate them as integer values, so you need the conversion. numbers =…
-
5
votes2
answers1193
viewsA: Is there any way to overwrite a specific line of a text file using Python?
The easiest way for you to do that, in my view, is to create a buffer temporary to store the contents of the final file, replacing the line that you want to change. The logic is simple: open the…
-
1
votes1
answer111
viewsA: Find set of numbers in a list that together add up to X
The module itertools has the function combinations, which generates a certain combination from the elements of an eternal object. That is, when we do combinations('stack', 3), we will have all…
-
3
votes2
answers474
viewsA: Print of the result of a ternary operator
In doing len(nome) > 'Braga' you will be comparing an integer number, which will be returned by the function len, with a string. This doesn’t make any sense, even gives the type error saying that…
-
0
votes1
answer314
viewsA: Renaming file: 'str' Object has no attribute 'group'
You are converting the function return search for string meaningless. for TXT in name_files: with open(path_txt + '\\' + TXT, "r") as content: search =…
-
1
votes1
answer103
viewsA: Equivalent to the "console.dir()" function in Python
So I believe what you need is the module inspect. In this module there is the function getmembers that returns a list of name/value pairs of all members of an object. To check what is what, you can…
-
7
votes2
answers2096
viewsA: How to make the system display an error message when it is not number?
The class int raises an exception of the type ValueError when the value to be converted to integer is not numerical, therefore, to ensure that the value entered by the user is numerical, just treat…
-
0
votes3
answers1636
viewsA: Start numeric variable with null value in Python
I particularly don’t like the solution of reading the value before the loop and reading it again inside the loop. These are lines of equal code, which become two places to edit if the application…
-
9
votes2
answers2285
viewsA: How to verify that the email is registered when typing in the form?
It is possible to do only with Javascript? It is not possible to do this only with Javascript, as you require access to the database to verify that the record exists. That is, some language should…
-
2
votes3
answers1051
viewsA: Identify a numerical sequence in a text file
One solution is to seek value through regular expression. To satisfy both possibilities, you can set as optional the presence of dots and hyphens between digits. It would look something like:…
-
3
votes1
answer183
views -
3
votes2
answers204
viewsA: Printar 2 interlaced strings
Just combine the functions zip_longest and chain, both of the module itertools: from itertools import chain, zip_longest string1='aovces' string2='m o' interpolation = zip_longest(string1, string2,…
-
5
votes2
answers1259
viewsQ: What are the function of each string prefix in Python?
In Python, we often see strings with a prefix, such as below, where the prefix is used r: r"""OS routines for NT or Posix depending on what system we're on. This exports: - all functions from posix…
-
2
votes1
answer356
viewsA: How do I list all the properties of an object?
You can iterate over all the class components of your object and search for those that are of type property. To get all the components, we can use the function dir, which will return both class…
-
12
votes3
answers168
viewsA: Why is it necessary to create a function to execute certain methods?
In the first case, .onclick = function() {myFunction()}; You are assigning to the event onclick the definition of a function. In this case, an anonymous function which, when executed, will call the…
-
2
votes1
answer54
viewsA: How to allow only the creation of valid objects in Python?
As Jefferson commented in the question, you can make an exception when the set number is invalid, in this case, less than zero. The exception will divert the flow of the program by delegating the…
-
3
votes1
answer259
viewsA: Calculate occurrences in the overlapping string
Your logic, though a little strange, is almost correct. The only problem is that due to overlapping substring occurrences in the string, its code is lost in the counters. You implemented that…
-
0
votes2
answers512
viewsA: Check empty fields with foreach
It is very important, both in terms of usability and performance that you at least place the property required in your HTML in the required fields. This will cause the user to be informed in real…
-
1
votes1
answer1914
viewsA: Passing a list of objects to a python dictionary
Yes, it’s wrong. Notice that you’re setting cont = 1 within its loop of repetition, then at the beginning of each iteration, the value of cont returns to 1, always adding the object in the same…