0

Multiples

#here’s another broken piece of code #the result is always too low num1=int(input()) num2=0 for i in range(num1): if (3*i) <= num1: num2 = num2 + (3*i) elif (5*i) <= num1: num2 = num2 + (5*i) print(num2)

30th Sep 2023, 12:26 AM
Annihilate
Annihilate - avatar
12 Respostas
+ 4
Trey , how can you expect to get help without giving a clear task description? what should the code do?
30th Sep 2023, 7:45 PM
Lothar
Lothar - avatar
+ 1
Please show your expected result. Syntax wise the code runs without error, and nobody knows how "the result is always too low."
30th Sep 2023, 1:52 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Trey in that case then your logic is wrong. If using 3*i and if 5*i then only one will ever be be hit since it is an if, elif. If you change the elif to an if, then it would simultaneously calculate 5*i. Probably want to store that in another variable though. You would need to do something like Stefanoo suggested. You would also need to specify which number you want multiples of, 3 or 5. Or track both with different variables and then verify that they are less than num1. You could also do a while loop instead.
1st Oct 2023, 12:10 AM
The Darkness
The Darkness - avatar
0
Trey I am not sure what the result should be, but if you switch the conditions around then you will get a higher number. If(5*i) .... Elif(3*i) ....
30th Sep 2023, 12:52 AM
The Darkness
The Darkness - avatar
0
It's like the others already said without content we don't know how to help. But you can write for i in range(1, num1): ... Because the first number will be 0 and this has no effect in your code. You could use * instead of + to get higher results. But this is just wildly out of the air. num2 = num2 * (3*i) or shorter num2 *= (3*i)
30th Sep 2023, 6:53 AM
Stefanoo
Stefanoo - avatar
0
It did get bigger, but not enough The Darkness
30th Sep 2023, 6:27 PM
Annihilate
Annihilate - avatar
0
Input is 100, output should be 2318, but is 2103 Wong Hei Ming
30th Sep 2023, 6:28 PM
Annihilate
Annihilate - avatar
0
Nothing changed, Stefanoo
30th Sep 2023, 6:30 PM
Annihilate
Annihilate - avatar
0
It should calculate the sum of all the multiples of 3 or 5 below a given number
30th Sep 2023, 8:50 PM
Annihilate
Annihilate - avatar
0
Can you give a smaller input, says 20, your expected result, AND the steps how num2 is calculated line by line as you expected? Most likely some numbers are missing, such as 15 because it can be multiply by 3 AND 5. Also, even the input is 100 your code return 1683, neither 2318 or 2103.
1st Oct 2023, 12:16 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Trey I think that you math may be off of you are expecting the results that you mentioned. For multiples of 5 we would have the following: 5, 10, 15 ... 90, 95, 100 This would result in 20 multiples, giving us 10 pairs of 105. 105 * 10 = 1050 Multiples of 3 would give us the following: 3, 6, 9 ... 93, 96, 99 This would result in 33 multiples, giving us 16 pairs of 102 and a median of 51. 102*16+51 = 1683 https://code.sololearn.com/cBdMMaOwsq9h/?ref=app
1st Oct 2023, 3:09 AM
The Darkness
The Darkness - avatar
0
ok thx
1st Oct 2023, 7:50 PM
Annihilate
Annihilate - avatar