+ 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:)
4 Answers
+ 7
Please try code playground.
+ 6
I copy paste it for you here:
https://code.sololearn.com/cz0c3wPbXqMi/?ref=app
0
I dont get it. Can u explain how this program works?
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