Posts by Woss • 73,416 points
1,476 posts
-
3
votes2
answers522
viewsA: Remove function in one list alters elements in another Python3
In Python, lists are mutable, which means that when you pass a list per parameter to a function, you will not copy the object, but pass the object itself. Any modification in the object within the…
-
4
votes3
answers191
viewsA: How to create a dictionary with a word and its adjacent from a string?
To catch each word and its adjacent, we can divide the string in the blanks and use the function zip to group them in pairs: texto = "We are not what we should be We are not what we need to be But…
-
10
votes4
answers418
viewsA: Is there a difference in using HTML5 attributes with or without true/false?
Such attributes are called boolean attributes - and, unofficially, in some places, owned. As stated in the specification, only the presence of the attribute is already sufficient to consider as…
-
13
votes3
answers1622
viewsA: What are the allowed elements within the <P> tag?
The element <p> is categorized as flow element and palpable element, can therefore be used anywhere an element of flow is expected. It allows as child elements any element that is a phrase…
-
1
votes2
answers110
viewsA: Output not expected in CSV Python (infinite loop) file
Its function to write a dictionary does not make much sense. def WriteDictToCSV(csv_files, csv_columns, dict_data): try: with open(csv_files, 'a') as csvfile: writer = csv.DictWriter(csvfile,…
-
2
votes3
answers353
viewsA: How to create a accumulator
If you have a list of students' grades in the classes, just check how many values are greater than 60. You can do this with the function len() and filter notes using list comprehension: notas = [60,…
-
2
votes1
answer4270
viewsA: Randomly draw letters
What you need to do is basically call the function random.choice the number of times you want to draw a letter. How you need 4 letters: 4 times. import random import string letras =…
-
7
votes3
answers1699
viewsA: What HTTP status returns to an expired token?
Short answer If the token is part of the body of the request, 403 (or 404), but is sent as token authentication, 401. 400 Bad Request I definitely wouldn’t go with an answer 400: Bad Request. This…
-
6
votes1
answer238
viewsQ: What does the asterisk represent in the definition of a function in Python?
For example, in the module documentation pickle it is common to see such a notation: pickle.dump(obj, file, protocol=None, *, fix_imports=True) pickle.dumps(obj, protocol=None, *, fix_imports=True)…
-
7
votes1
answer238
viewsA: What does the asterisk represent in the definition of a function in Python?
The answer you are looking for exists and is called PEP. Legal Pepe? No, PEP 3102. PEP 3102 -- Keyword-Only Arguments As the title of PEP already reported, this syntax allows defining functions that…
-
2
votes3
answers2181
viewsA: Python functions-Global and local variables
There she is, returning her. There are defined and "rigid" scopes because it’s the best environment - or at least it makes life much easier for the developer, because the smaller its scope, the…
-
4
votes1
answer83
viewsA: Load 4-in-4 items into a javascript loop
What you need is what they call Chunks, which is basically to divide a list into smaller lists of the maximum size defined. To do this, just use the method slice of Array to get each slice of your…
-
1
votes2
answers335
viewsA: Modal Form Login appears once and disappears at the same time
First, it makes no sense for you to put an element <li> within an element <a>: <a href="" id="signin"><li>Entrar</li></a> This is not allowed by W3C and WHATWG…
-
4
votes1
answer2777
viewsA: Python file comparison program
As commented, the problem of reading only the first line is that you used the method readline, that reads only one line. To read them all, you will need to iterate over the file with a repeat loop.…
-
2
votes1
answer497
viewsA: What is Ruby for "ensure"? Give an Example
The ensure of Ruby is equivalent to finally of other languages: it defines a block of code that will always be executed, either after the begin, is after catching an exception. begin puts 'Bloco de…
-
1
votes3
answers1041
viewsA: Sort sequence of numbers from a TXT file
To work with files, I advise you to use contextual managers: What’s with no Python for? with open('arquivo.txt') as arquivo: linha = arquivo.readline() However, linha will be a string and, to sort,…
-
3
votes1
answer49
viewsA: If inside the Loop does not work properly
This possibly happened because the reference of i will be the same for all requests. As these are asynchronous requests, the loop will end quickly, reaching the maximum value of i, which in this…
-
0
votes2
answers415
viewsA: Call list in function in python 3
Error says you called the function remove_repetidos, that expects an argument with no arguments. To fix this, just inform it properly. However, the new list will be returned by function, then you…
-
4
votes3
answers8232
viewsA: A register of people in python
I believe that the problem has been solved in the other answers, but I will leave here a suggestion of how I would solve the problem if you are interested to study an alternative. First, you have 4…
-
8
votes5
answers11991
viewsA: How to validate and calculate the control digit of a CPF
A solution more pythonica would be: def validate(cpf: str) -> bool: """ Efetua a validação do CPF, tanto formatação quando dígito verificadores. Parâmetros: cpf (str): CPF a ser validado Retorno:…
-
1
votes4
answers685
viewsA: Only put the correct message print once
You are scrolling through each character of the CPF and displaying the message to each one. If the CPF has 14 characters, the message will appear 14 times. It doesn’t even make sense to do so. In…
-
3
votes1
answer35
viewsA: generation of a vector through algebraic operation
The simplest way is using comprehensilist on to generate the new list: tensao = [1, 2, 3, 4, 5] defo_calculada2 = [t + 0.2 for t in tensao] print(defo_calculada2) # [1.2, 2.2, 3.2, 4.2, 5.2] If…
-
1
votes1
answer24
viewsA: Problems when displaying date in appropriate format
In accordance with the documentation, the formatting M is a representation textual short of the month name (three letters); on the other hand, the formatting m is the representation numerical of the…
-
14
votes2
answers475
viewsA: Why in Python 0.03 % 0.01 = 0.009999999999998 and not 0?
Exactly for the same reason that 0.1 + 0.7 is 0.7999999999999 and not 0.8. Inaccurate result in broken numbers calculation Calculation of incorrect multiplication Integer conversation in the…
-
23
votes2
answers52286
viewsA: Which operator is equivalent to different in Python?
As with most languages, the difference operator in Python is !=. Remember that it compares only the value between the operands and not their identities. a = 2 b = 3 if a != b: print('a é diferente…
-
1
votes4
answers6679
viewsA: How to force numeric keyboard display on the smartphone when clicking an input?
Remember that ZIP code is a text that is composed with numeric characters, so it makes no sense to use the field number. As already commented, in HTML 5 there is the attribute inputmode that can be…
-
5
votes2
answers76
viewsA: Dismantle number with zeroes
With regular expression, you achieve this in a trivial way: $text = "0002100042000560000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; if…
-
3
votes1
answer31
viewsA: php method that checks two strings in a variable
Apparently, you are considering that writing twice the variable is to insert redundancy into the code. On the one hand it is, but it makes the code simpler. If you still want to change, just use the…
-
4
votes1
answer125
viewsA: Use . php instead of . js
It is possible to do this, but with PHP you need to inform that the HTTP response you will return will be a JS code so that the client (browser) can interpret correctly. In fact, the file extension…
-
1
votes3
answers5999
viewsA: Inserted in a list of lists in python
First, by the problem reported about lists being at the same values, you are getting started wrong. If you do: >>> lista = [[]]*13 >>> print(lista) [[], [], [], [], [], [], [], [],…
-
5
votes2
answers514
viewsA: How to have two counters in the same loop?
You need to use Slices to access parts of string together with the function zip: texto = 'Stack Overflow em Português' for a, b in zip(texto, texto[1:]): if a == b: print('Há caracteres iguais em…
-
1
votes2
answers56
viewsA: If block is not well located, how to solve?
As I commented, it makes no sense for you to call the function to the door 'aaa'. I tried for the comments, but it seems to have been inefficient. Your code is like this: if MeuScanDaorao(port):…
-
5
votes1
answer10618
viewsA: How to print information in python table format?
Just use the method format of string: >>> '{:>13,.2f}'.format(100000) ' 100,000.00' Where: The keys, {}, define a group referring to the parameters of format; The two points, :,…
python-3.xanswered Woss 73,416 -
1
votes1
answer1548
viewsA: Password Validation with JS
The main problem that makes your code not work is by using getElementById for elements that do not have the attribute id defined. That is, to keep the code so, just define the attributes in the…
javascriptanswered Woss 73,416 -
4
votes3
answers1369
viewsA: Calculate the sum and mean of a numerical sequence
Note: it is important to note that there are infinite number series that satisfy the sequence {3,5,7,9,11,13,15,...} (view end of reply), then ask the user to identify the standard makes no sense…
-
3
votes2
answers130
viewsA: Filter through the href
The main problem with your code is that you are trying to access the property href of a set of values. The function getElementsByTagName, as its name says, it brings a list of elements by the name…
-
0
votes1
answer116
viewsA: I need to read a . csv file and rewrite it into another . csv file without stopwords using Python
I believe the main problem is that you are reading and analyzing the entire contents of the file, and you want sentence by sentence. Then, to resolve, you should read each line of the input file…
-
2
votes2
answers584
viewsA: How to stop a loop at a certain time?
Just as I replied in How to perform a task at a precise time in time? You don’t need to create a repeat loop and keep checking the time every second, you just set the call schedule to a function at…
-
5
votes1
answer2542
viewsA: Adding elements from a list to a dictionary in Python3
Your code generates a wrong result because the value of y will always match the elem in the list and thus will always be the same value. To implement something of this kind, you need to scroll…
-
2
votes1
answer64
viewsA: How to know if the list contains so many elements that meet and do not meet a condition, using stream?
A slightly simpler logic would be to take the value of the first element of the list and just check if there is any other one in it. If the list is homogeneous, all values will be equal and…
-
4
votes2
answers1389
viewsA: Fetch a value within an array of dictionaries
You can use the function filter: resultado = filter(lambda termo: termo['palavra'] == 'sim', palavras) The result will be an eternal object (generator) with all dictionaries that have 'palavra'…
-
2
votes1
answer1354
viewsA: How to make a timer in Python 3?
As I quoted in the answer below Timeout in Python input function You can use the module signal, on Unix systems, to call a function after a certain time, without blocking the execution of the main…
-
0
votes3
answers465
viewsA: Test sum function
You can also use the module doctest. With it you can create the direct tests on docstring of the function, which centralizes the tests together with the function and assists the documentation of the…
-
2
votes1
answer906
viewsA: Limit when creating a list in Python
Because what you’re adding to the list is another list of 1000 elements, but even if you have 1000 elements, it only counts as one - because it’s only a single list. In doing: >>> lista =…
-
1
votes2
answers697
viewsA: How to add list values?
Assuming you already have the data separated by state, we can define a function that sums the crime rates: def soma_criminalidade(estado): return sum(cidade[3] for cidade in estado[1]) But since the…
-
1
votes1
answer48
viewsA: How to take the value of multiple items and change according to my if
In your code: function isFree(){ var preco = document.getElementsByClassName('comprar'); for(n = 0; n < preco.length; n++){ var validaPreco = preco[n].innerHTML; if(validaPreco == '$ 0'){…
-
0
votes1
answer373
views -
4
votes1
answer50
viewsA: Generate file with JS
You can define an anchor element, <a>, using the URL encoding to generate your TXT file and download it. Just create the element dynamically, set the attribute href and the property download…
javascriptanswered Woss 73,416 -
1
votes2
answers905
viewsA: Add values from the same PHP array
If the idea is to add together the elements of array internal element by element, so just do: $result = array_map(function(...$values) { return array_sum($values); }, ...$arr); So, by having the…
-
0
votes1
answer52
viewsA: For juntamante with the while
If the intention is just to insert the numbering together with the title, just set an auxiliary counter and interpolate: $contador = 1; while($reseb = $existebonus->fetchObject()){ $titulo =…