Posts by Solkarped • 2,428 points
218 posts
-
1
votes2
answers73
viewsA: How to print the number that appears odd times (Python 3)
From what I understand you want to implement a script that is able to display the value(s) whose(s) count of your occurrences is an odd number. Well, for that we must make the following logic: Take…
python-3.xanswered Solkarped 2,428 -
0
votes3
answers94
viewsA: String validation using while in Python
If you are using python 3.8 or higher, may implement Assigment Expression (Assignment expression) in your code to perform the checks. This way the code would look: while (s := input('Sexo: [M/F]…
-
2
votes3
answers157
viewsA: How to add digits from a number to "1" digit?
Another interesting way to resolve this issue is to associate Assignment expressions along with Generating expressions. In this case the code would be: x = input("Digite um número: ") while (s :=…
-
3
votes3
answers9505
viewsA: Difference between dates/times in Python
Another interesting way to resolve this issue is by using the library dateutil. That way we can implement the following code: from datetime import datetime from dateutil.relativedelta import…
-
2
votes2
answers102
viewsA: My code is only returning half of what it should
One of the ways you can solve this question is by using one of the sorting algorithms. In this question I will give two examples using the sorting algorithm bubble sort. The first algorithm will use…
-
0
votes2
answers137
viewsA: A proposed infinity loop PYTHON exercise
Another interesting way to resolve this issue is by implementing a class encompassing all the means we desire. This way we can implement the following code: class Medias: def __init__(self, num1,…
-
0
votes3
answers300
viewsA: How to use while and if in Python?
There are some errors in your code. The first mistake - and perhaps the most important - is that you need to wrap your code in a loop. In this case, the most indicated loop is the while True. The…
-
1
votes1
answer74
viewsA: How to generate a new list from another Python list?
From what I understand you want to get the list frequency and apply the formula... (7.19 * (10**(-3)) + (6.09/((freq**2)+0.227) + (4.81/(((freq-57)**2)+1.5)))) * freq**2 * (10**(-3)) ... to each…
-
1
votes1
answer104
viewsA: How to change the background of a chart with Python 'Seaborn'
You should delete the following line of code: sns.set(style='darkgrid') Then you should add the following line of code before building the chart. sns.set_style("darkgrid") This way the complete code…
-
1
votes3
answers104
viewsA: How do I make Python not turn to the upper or lower case letters of the code?
Note that this program, just as the author himself said, is a bad joke. The purpose of this program is, if the user enters the answer CERTAIN the programme is re-dispatched. And, if the response is…
-
0
votes2
answers88
viewsA: Intersection of words with Python dictionaries
According to the third sentence of the question... Your program should contemplate a function that receives the strings read and returns a dictionary wave the keys are composed by the words and the…
-
0
votes2
answers69
viewsA: How to replace file extensions?
Since you need to replace one extension with another you should work with string slicing. Note that the extensions you need to replace have 4 characters who are; .hpp. this way you need to go…
-
0
votes2
answers50
viewsA: Copy Files using shutil library
I understand you are trying to implement a code that is able to copy a file from one directory to another by renaming so that the new name contains the old name, plus the character of "_", plus the…
-
1
votes1
answer55
viewsA: How to compare if a number entered by the user is equal to a certain value
When you do: idade = input('Qual a sua idade? ') You are assigning a string to the variable age and therefore your code will never run the if. Yeah, the if shall be executed only if the age be the…
-
1
votes3
answers69
viewsA: Matplotlib sorting using index as bar denominator
One of the ways you can get the desired result is: import matplotlib.pyplot as plt plt.style.use('seaborn') des = ['0. item A', '1. item B', '2. item C', '3. item D','4. item E','5. item F'] total =…
-
-2
votes2
answers403
viewsA: Compare matrix values and add them to another (python)
One of the valid ways to resolve this issue is: matrixOne = [[1, 12, 45], [58, 524, 78], [1, 2, 3], [45, 456, 8]] matrixTwo = [[0, 1, 2], [85, 1, 74], [27, 63, 21], [25, 47, 962]] matrixTrhee = []…
-
1
votes2
answers456
viewsA: Print data from a list that is inside a dictionary - Python
To resolve this issue you should pay attention to the following logic: Capture the person’s name; Make a list of the person’s notes; Feed dictionary with name and notes; View key, dictionary value.…
-
1
votes2
answers266
viewsA: FILL MATRIX WITH PYTHON RANDOM VALUES
We can manipulate matrices in several ways. One of the most interesting is using the library numpy. This library has several methods specialized in matrices. Well one of the ways you can solve such…
-
1
votes4
answers84
viewsA: Error when using index to pick up a specific index
From what I understand you are wanting to implement a code that is able to assemble a list with 5 values, check the highest value element and then specify the index of this value. Well, when you do:…
-
0
votes2
answers160
viewsA: Help with PYTHON lists
The statement requests a FUNCTION that returns only the DIGIT VERIFIER according to certain rules. One of the ways we can assemble this function is: def digito_verificador(n): soma = 0 o, p =…
-
4
votes3
answers220
viewsA: How to popular a dynamically key and value dictionary?
To resolve this issue you must implement a dictionary - whose delimiters are {} - and not a list - whose delimiters are []. One of the ways you can use it is Dictionary comprehensions PEP 274. Using…
-
0
votes3
answers123
viewsA: Problem when denying receipt of a certain value
Just to be clear, the statement of the reply of Maniero, already answers the question. However, there are several ways to calculate the arithmetic mean between two numbers. If you’re using Python…
-
0
votes4
answers7124
viewsA: Accept only numerics in input
Another interesting way to resolve this issue, with Python 3.8 or higher is using the concepts of Assignment Expressions, approached by PEP 572. With these concepts we can greatly reduce the number…
-
0
votes2
answers75
viewsA: Help to find the square root of a positive Python number
In this code there are two errors. The first refers to the function print in the code line of input() - you must remove it. And the second mistake was that you called the library Math without…
-
1
votes3
answers9053
viewsA: Multiplying terms from the list
If you are using the Python 3.8 or higher, you can make use of the method Prod library Math. This method is used to calculate the product of the elements of an eternal. This way the code can be…
-
0
votes2
answers57
viewsA: How to use a variable function several times without creating another variable? How to draw a number
As I understand it, you are trying to create a little guessing game. Where the computer will draw a value between 1 and 6 and the user will try to discover the value drawn by the computer. Well, to…
python-3.xanswered Solkarped 2,428 -
1
votes3
answers144
viewsA: Calculate the value of a multiplication using only summation and subtraction operators
The operation of multiplication is nothing more than successive additions of a certain value. This way the multiplication operation can be mounted as follows: Multiplying x Multiplier = Product In…
-
0
votes2
answers94
viewsA: how should I do for the python code I made for lottery did not repeat the numbers on the same when I put 5 pair and 1 odd to give the game result
From what I understand you want to generate groups of numbers that will be used as guesses in lottery games, where you must specify the amount of values pairs and also odd values. Well, first of all…
python-3.xanswered Solkarped 2,428 -
4
votes4
answers1234
viewsA: Python - Calculate transposed matrix
First, for you to calculate the transposed of an "A" matrix, you need the "A" matrix. Another thing, for you to calculate the transpose of a matrix with the help of the function map(), you will need…
-
5
votes3
answers174
viewsA: Python - Sine cycle in degrees
For starters, the circular functions - sine, cosine and tangent - are obtained through the trigonometric circle. This, in turn, goes from 0 to 360°. So it makes no sense to table the sines in the…
-
5
votes3
answers110
viewsA: How to "flatten" a list of lists of integers?
To transform d in s you can implement two loops of nested repeats. In this case the code would be: d = [[1], [2], [3], [3]] s = list() for i in d: for j in i: s.append(j) print(s) In this code the…
-
0
votes4
answers17968
viewsA: Remove elements from a Python List
One of the valid ways to solve this problem is by performing a deep copy of list one. This process can be obtained using the method deepcopy library copy OBSERVING: A deep copy builds a new…
-
1
votes3
answers9942
viewsA: Remove non-numeric characters from a Python string
Another possible way to resolve this issue is by using the function filter associated with lambda. This way the code would be: s = input('Digite uma string: ') resultado = ''.join(filter(lambda i: i…
-
1
votes6
answers37862
viewsA: How to remove characters from a string?
An interesting way to resolve this issue is by using the function filter along with lambda. For this we can assemble the code as follows: string1 = 'chocolate' string2 = 'oca' resultado =…
-
1
votes6
answers42162
viewsA: Removing duplicate elements in a python list
Another interesting way to resolve this issue is to use the method Ordereddict library Collections. In this case we can assemble the following code: from collections import OrderedDict def…
-
2
votes3
answers253
viewsA: How to resolve "Valueerror: max() Arg is an Empty Sequence" error when the list is empty?
The mistake "Valueerror: max() Arg is an Empty Sequence" is indicating that some - or all - sequences are empty. For you to fix this problem you should change the block... if None in classe1 or…
-
1
votes4
answers6021
viewsA: How to create a function to find the highest value in a list?
One of the interesting ways to solve this issue is to use an anonymous function (lambda). In this case, the logic should be set up as follows:: Import the library functools along with your method…
-
0
votes3
answers16319
views -
0
votes5
answers10407
viewsA: Identify if there is an uppercase letter in the string
Another interesting way to resolve this issue is by implementing a function of the type def. In this case the code would be: def maiuscula(p): if any(i.isupper() for i in p): return True return…
-
0
votes4
answers8851
viewsA: Full Python file path
Another interesting way to resolve this issue is by using the following code: from os import chdir, listdir from os.path import isfile cam = input('Digite o caminho: ') chdir(cam) soma = 0 for c in…
-
1
votes3
answers12860
viewsA: How to fill left zeros in Python?
Using f-strings (literal strings) the documentation of which is here and here, we can use the following code: a = 4 print(f'{a:04}') Running this code we will receive the following output: 0004 Then…
-
0
votes3
answers1630
viewsA: Problem with randint function
The best strategy to draw whole numbers, random and without repetitions is using the method sample library Random. In this way we can use the following code: from random import sample list_num =…
-
1
votes2
answers14794
viewsA: Identify repeated elements in Python list
Another interesting way is using the method Counter library Collections. In this case, the code would be: from collections import Counter lista = list(map(int, input('Digite os valores: ').split()))…
-
1
votes6
answers342
viewsA: Get a list of the first multiples of a number
From what I understand you want to implement a script that can find and list the 5 first multiples of 3. Well, to solve this problem we can use the following code: lista = list() for i in range(3,…
-
1
votes2
answers274
viewsA: Limit the amount of decimals of a floats values that fill a list
To display the list values with a number of specific decimal places you can use the function round(), specifying the number of decimals. In this case the code would look like this: import pandas as…
-
1
votes3
answers73
viewsA: Doubt about storing the function result in a list and the types of stored variables
If you want to calculate the percentage change, first you should know the logic of the calculation. The formula for calculating the percentage change is: variação percentual = ((vf / vi) - 1) * 100…
-
4
votes2
answers140
viewsA: How to generate repeated guesses for the mega sena game?
From what I understand you want to implement an algorithm that is able to generate game guess from Mega Sena. Well, as I said in the comment above, we should use the method sample library random. A…
-
4
votes4
answers167
viewsA: How to return the first vowel of several words in a list?
Another interesting way to resolve this issue is by using the following code: palavras = ('boi', 'vaca', 'macaco', 'ovelha', 'lesma') for palavra in palavras: for letra in palavra: if letra in…
-
1
votes3
answers71
viewsA: You are not printing string
From what I understand, you want to implement a script that is able to receive a Frase and return this phrase with the letters of each word reversed, that is, receive "I like programming" and return…
-
2
votes2
answers355
viewsA: Float to str conversion without losing two decimal places and changing decimal separator
If you are using IDLE may use the following code >>> str(f'{float(10.00):.2f}').replace('.', ',') In this case the exit will be: '10,00' Now if you want to implement a script you can use…