Posts by Woss • 73,416 points
1,476 posts
-
1
votes3
answers188
viewsA: Read two notes and display a string according to the average between them
Your code produces the expected output, as can be seen here: https://sopt-question-381571.acwoss.repl.run. But there are considerations that will be important: The initializing method of float,…
-
4
votes2
answers272
views -
3
votes2
answers544
viewsA: Yellow comment on Pycharm when using the term "all"
This is due to the settings of Pycharm, which considers the term "whole" as to of, of the English "to do" and highlights the content as a way to alert the developer about the pending. Even you can…
-
1
votes2
answers336
viewsA: Typeerror: not all Arguments converted During string formatting (python)
It turns out that the function return input is always a string: input([prompt]) If the prompt argument is present, it is Written to standard output without a trailing newline. The Function then…
-
3
votes1
answer382
viewsA: Remove empty list in Python
It doesn’t work because the logic of your code is wrong. The object lista begins with the value [], an empty list and only changes, that is, a new element is added if the condition if lista != [] is…
-
2
votes2
answers93
viewsA: How to validate return of a method like php7 object array?
PHP, unlike Java, has dynamic typing. A variable that is now string can receive an integer and change its type without problems and this, inclusive, is expected from the language. This is possible…
-
4
votes3
answers4508
viewsA: Space between result (Python)
If you don’t mind leaving a blank at the end of the string displayed, just you concatenate (or interpolate) a blank space after the digit that was informed by the user: n1 = input('Digite um número:…
-
6
votes3
answers2910
viewsA: How to delete the first line in a python CSV file
To save a dataframe without the header, just do df.to_csv('arquivo.csv', header=False) If you also want to remove the indexes df.to_csv('arquivo.csv', header=False, index=False) So, if you have a…
-
14
votes3
answers559
viewsA: What’s the best way to play a Loop in math?
The way it is: public int E () { int e = 0; for(int i = 0; i < 5; i++) { e += i; e *= i + 2; } return e; } The function E will always return the value 714, then the mathematical representation…
-
3
votes3
answers182
views -
2
votes3
answers231
viewsA: Do require_once within the classes. Is it possible?
One thing you need to be clear about is that include (or require) is different from import. When you do the include of a file on index.php, for the PHP interpreter it’s like you copy all the code…
-
3
votes1
answer352
viewsA: How to save a function, with predefined parameters, in a python variable?
You can do this through the function functools.partial: from functools import partial somar_um = partial(somar_iguais, referencia=1) somar_dois = partial(simar_iguais, referencia=2) So in doing…
-
1
votes1
answer919
viewsA: Python recursive function that returns the largest and smallest value within a list
You got too close to the solution. You only missed returning when the number in the list is greater than 1. You calculate the minimum and maximum of the rest of the list, but do nothing with the…
-
6
votes1
answer347
viewsQ: What are dataclasses and when to use them?
In version 3.7* calls were added dataclasses which were designed from the PEP 557 and consists of using a decorator, dataclass, in "normal classes". What are these dataclasses? What are the benefits…
-
4
votes1
answer63
viewsA: How to assemble a set of different data types into a named structure in python?
For Python 3.7+ you can use the dataclasses.dataclass: from dataclasses import dataclass @dataclass class TModo: descricao: str valor_i: str valor_f: str versao: int Due to the presence of the…
-
2
votes2
answers73
viewsA: Notice: Undefined index: Dentifier does not find the error
It seems that the error is in the misinterpretation of the HTTP protocol. What the function setcookie does is set a response header Set-Cookie in the HTTP response that will be sent to the client;…
-
1
votes1
answer1037
viewsA: Create successive sum multiplication program in Python
The problem is that you made the process too complicated trying to solve all the items at once. The result of this is dirty code, with a lot of repetition of logic that has all the potential not to…
-
5
votes2
answers723
viewsA: Method "Sort" returning only "None", what is the reason?
The method list.sort modifies the own llista, does not return a new object as it occurs with the function sorted. This way, you are overwriting the object lista with the return of list.sort, that is…
-
3
votes2
answers51
viewsA: Capture value within the string
/([^\(]+)\(?([^\)]+)?\)?/ ([^\(]+), creates a capture group for any character other than (; \(?, sets the literal character ( as an optional; ([^\)]+), creates a capture group for any character…
-
1
votes1
answer43
viewsA: Separate Array into groups and insert them into a pre-defined text
You can remove the brackets and separate all values by comma: $texto = '[Molho de Tomate, 12, 4.59, Pipoca, 5, 7.90]'; $valores = explode(',', trim($texto, '[]')); So you’ll have something like:…
-
3
votes2
answers86
viewsA: Code return problem (second-degree equation)
Many strange things, but the nan gave up because you did delta(a,c,b), reversing the values of b and c. This is possibly implying a negative return from delta and thus try to calculate the square…
-
4
votes4
answers2298
viewsA: Printing a number odd sequence on a list
The signature of range is: range(start, stop[, step]) If you want to list all odd numbers in the range [0, n], you just have to analyze that the first odd number is 1 and always the next one will be…
-
2
votes2
answers61
viewsA: Random questions (id) in a record
First, if you need the primary key of the question, you do not need to select all the columns in the database, just select the one you will use. Also it is not necessary to make a repeat loop to go…
-
3
votes3
answers1015
viewsA: The sum of all non-prime odd numbers that precede an integer?
The simplest way to implement it is: Read the number n; Starts sum to zero; Traverse the range [1, n] in k; Checks if k is a prime number. If yes, it goes back to 3; if it does not continue; Check…
-
7
votes3
answers1885
viewsA: Longest word in a sentence in the list - Python
First, the line below is completely unnecessary: maxstring = list(map(str, text)) When you do the split of a string you will already get a list of strings. Make a map to convert them into string…
-
1
votes3
answers116
viewsA: URL does not display symbols
This is called URL encoding and serves to avoid conflict between the contents URL with control characters. For example, URL parsers use the bar, /, to delimit the segments of the path, as well as…
-
1
votes2
answers48
viewsA: String sub-stituir per variable
In this line line = line.replace('X','c3',1) You are replacing 'X' for string 'c3', not at variable value c3 as you wish. To access the value of a variable you should not use quotes. You probably…
-
2
votes3
answers503
viewsA: Combination of values for sample selection
The error is basically because you are trying to convert a tuple into a whole. Basically doing something like: int((2, 5, 7)) And the class int don’t know what to do with a tuple. I found some…
-
2
votes1
answer79
viewsA: Composer autoload
You just included the namespace Controllers, then to use this class namespace you aind appreciate referencing it: use App\Http\Controllers; $routes = new Controllers\Routes(); If you want to…
-
5
votes2
answers1897
viewsA: Django - Null and Blank Difference
As to the null is correct. When using null=False the column will be on the bench as NOT NULL; the contrary is also valid. As to the blank, it will define the behavior of the application when the…
-
1
votes1
answer89
viewsA: Error accessing array indices by name( foreach-php )
Remember that PHP is case sensitive; if you create in the database the column TIPO_ATIVIDADE then PHP will generate the key in array also with uppercase text. It makes a difference to access the…
-
5
votes1
answer231
viewsA: Doesn’t Python3 recognize 0 as int?
Because zero is considered false. Just do if x will perform what we call Truth value testing and in this test it was defined that the numerical value zero will be considered false¹ . Thus, if…
-
1
votes1
answer39
viewsA: How to check an array quickly?
There are some libraries that already implement what you want. For example, particle/validator. To install through Composer: $ composer require particle/validator Thus, you can define your…
-
8
votes2
answers1136
viewsA: What is the purpose of using "Else" together with Try/except in Python?
To this example may not make a difference, but for others yes. The idea is always to keep the smallest possible code block inside the try you want to capture the exception. For example, imagine that…
-
2
votes2
answers60
viewsA: How to create variables dynamically (Speech_recognition)
If you know the amount of blocks, make a repeat loop that stores the values in a list: N = 43 audios = [] with sr.AudioFile(AUDIO_FILE) as source: for _ in range(N) audios.append( r.record(source,…
-
17
votes1
answer2734
viewsA: What is the difference between r+ and w+ modes in Python?
See the table on official documentation of the function open: That in summary: r open the file for reading (keep cursor at the beginning of the file); w open the file for writing, delete the current…
-
1
votes1
answer342
viewsA: Return word media in a dictionary from another function
In function conta_media_palavras you need to call the function contar_palavras. The way you did just set a new name for the same function, you didn’t call it. Instead of palavras = conta_palavras…
-
3
votes1
answer43
viewsA: Function explodes that ignores content inside quotes
Yes, use the right tools for the job. If you are processing a CSV file, use the function fgetcsv to read the content. Such a function has a parameter that you define string: fgetcsv ( resource…
-
6
votes7
answers620
viewsA: How to access a circular array?
An alternative would also be to have the option of setting a limit to its function, that is to say, repeating the values of the array up to a certain amount. For this, you could define a generator…
-
2
votes4
answers461
viewsA: How can a group of parameters be required if at least one of them is informed?
First, read the answers: Using decorators, by jsbueno Class of parameters, by Sidon But a specific solution to your problem - that is, something that is not generic and independent of function - is…
-
3
votes2
answers31
viewsA: Is it a problem to remove class that an element does not have?
No, no problem. What the function removeClass is basically analyzing the value of the attribute class of the element as string and replace all occurrences of the specified class with a blank space.…
-
6
votes2
answers134
viewsA: Comparison of array in php
You can use the function array_map: $array1 = [[1,2,3,4,5], [2,3,4,5,6]]; $array2 = [1,2,3,4,6]; $diferencas = array_map(function ($array1) use ($array2) { return array_diff($array1, $array2); },…
-
3
votes4
answers361
viewsA: Python Code - Conditional Structure
First, the "function" (which is a class) float does not read the program input. That is, you cannot ask the user a value through it, for this you will need the function input (raw_input in Python…
-
3
votes1
answer302
viewsA: Import chatterbot bot to Telegram
Chatterbot doesn’t define how you will get the phrase to be processed, so you just need to integrate with the Telegram API to pick up the phrase you typed and answer for it as well. For example:…
-
2
votes1
answer53
viewsA: Create an algorithm that removes and adds an item
Since you don’t want to use classes, you can structure your objects with arrays. Each player must have a name, basic attributes and an inventory. Items that are equipped are also in the player’s…
-
3
votes1
answer320
viewsA: How do I get the name with the file extension in a Python URL?
Just take the value of path of the URL and divide it into the bar character: from urllib.parse import urlsplit url = 'http://dominio.com.br/caminho/da/imagem/imagem.png' parts = urlsplit(url) paths…
-
1
votes2
answers72
viewsA: What is the most practical way to change the sign from a numeric variable to a positive one?
Calculating the absolute value of the number with the function abs: numero = abs(numero) By definition, the return of abs will always be positive. assert abs(1) == 1 assert abs(0) == 0 assert…
python-3.xanswered Woss 73,416 -
2
votes4
answers3572
viewsA: How can I distribute the . py program without the user having to install all libraries?
You can use the module zipapp in versions 3.5+. TL;DR Let’s assume that I will make an application that queries a user API. For this we will need the package requests. We will have the application…
-
0
votes1
answer734
viewsA: Regex in Python Validate E-mail
Understanding the expression you wrote: ^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.[a-zA-Z\.a-zA-Z]{1,3}$ |_____________| |__________| |____________________| | | | | | +-> (3) Buscará o resto do e-mail | | |…
-
2
votes1
answer49
viewsA: Improving Drag and Drop Code
You can define a function for this - the end, they exist for this: reduce duplicate code. Looking at your code very superficially, it seems to me that the only changes are of the elements that are…