0
Can someone explain this code?
https://code.sololearn.com/cnjGa9rzJEXl/?ref=app this is example given in my school book. And the most important thing 1. why i= num1 2.how the answer is correct when we're not even using num1*num2
3 odpowiedzi
+ 5
Instead of using a while loop and a counter varibale, it can also be done with a for loop and a range:
...
for i in range(0,num2):
product += num1
...
+ 1
i = num1 because it is the number of iterations the loop will undergo and you don't want to modify num1 so you use an extra variable i.
For example if the input is-
5
6
Then you keep on adding 6 for 5 times in the 'product' variable which is equal to 6*5 so the answer would be 30.
+ 1
At the start you get over input two numbers. First of them will be assigned to the variable i, which is used in the while loop. i is the number of times the second input number will be added to the variable product. For the reason the product equals to num1*num2.