Your code already works (for positive numbers, because if they are negative, you will need an adjustment, explained below). Anyway, we can improve a little...
Basically, multiplication is nothing more than a repeated sum several times. So just do:
var1 = # ler o valor
var2 = # ler o valor
result = 0
for _ in range(var2):
result += var1
print(result)
Or you can use sum
along with a Generator Expression:
result = sum(var1 for _ in range(var2))
print(result)
Which is basically the same thing.
Note that we don’t need the value of the variable in for
, so I called her _
(which is a Python convention to indicate that the variable is not used: it is only there to "satisfy the bureaucracy syntax" of the language, which requires having a variable there). But since I am only interested in adding the value of var1
a certain amount of times, whatever the variable of the for
.
Of course, there is another way - which should be more inefficient, by creating a list unnecessarily:
result = sum([var1] * var2)
Basically I create a list containing var1
, and by multiplying a list by a number, I give another list containing var2
times the contents of the list. In this case, we will have a list with the number var1
repeated var2
times, and just add it all up. Note that I even used multiplication here, but it wasn’t between the numbers ;-)
But as I have already said, this is unnecessarily more inefficient, by generating a random list (and becomes even more inefficient if the numbers are large, because then we will be generating huge lists without need).
There’s only one catch: this method only works if both numbers are positive, or if only var1
is negative. But if var2
is negative, there the range
no longer works, so we have to make a small adaptation:
# lê os valores de var1 e var2
if var1 < 0 and var2 < 0: # se ambos forem negativos, inverte o sinal (pois o resultado será positivo)
var1 = -var1
var2 = -var2
elif var2 < 0: # se somente var2 for negativo, troca de lugar com var1
var1, var2 = var2, var1
if var1 > 0 and var1 < var2: # se var1 não é negativo e for menor, também troca com var2
var1, var2 = var2, var1
# calcula usando um dos métodos acima
If both are negative, just reverse their signal (since the result will be positive).
If only var2
is negative, I change the value of it with var1
(thus, var1
becomes the negative number and var2
the positive).
Another case I change var1
with var2
that’s when var1
is smaller (but only if positive). This is an optimization for the case of var2
be much larger: for example, if var1
for 5 and var2
for 1 million, 1 million iterations will be made to add up the 5. But if I change their values, I will only make 5 iterations adding up to 1 million (of course if both are very large numbers, it won’t make as much difference).
And if you want, you can also include another case, when one of the numbers is zero, because then the result is zero and there is nothing to calculate:
if var1 == 0 or var2 == 0:
# se um dos números é zero, nem precisa calcular
result = 0
else:
# usa o código acima para calcular...
Your code, although it has some things to improve, is working perfectly.
– JeanExtreme002