+ 1

What ist the outcome of this Python Program and how does it work?

a=7 a=int(input("enter a number:")) z=0 for i in range (0,a,1): z=z+a*i if i<5: for j in range (0, i+1,1): z=z+j print(str(z)) Thanks for help:)

24th Feb 2019, 5:24 PM
Sal
4 Answers
+ 7
Please try code playground.
24th Feb 2019, 5:45 PM
Hubert Dudek
Hubert Dudek - avatar
+ 6
24th Feb 2019, 5:49 PM
Hubert Dudek
Hubert Dudek - avatar
0
I dont get it. Can u explain how this program works?
24th Feb 2019, 5:52 PM
Sal
0
First: a=7 is useless since you'll overwrite a with int(input()) Then you initialize z to 0 and you have a loop with i that goes from 0 to a with step 1 (you can just use range(a)); in every cycle you sum z to a*i and, if i is less than 5, you even add j (the counter of a new loop that goes from 0 to i+1 with step 1) to z. Then you output z, you can just use print(z). For the result: it obviously depends on the input. P.S.: in Python the max value is always excluded, so: for i in range(5): print(i) Prints: 0 1 2 3 4
24th Feb 2019, 10:23 PM
Federico Corradini
Federico Corradini - avatar