0

While Loops

Can someone please explain this problem to me? z = 2 sum = 0 while z<9: z += 1 sum=sum+z print (sum) Thanks

9th Dec 2018, 6:29 AM
Khk Gigi
Khk Gigi - avatar
11 Antworten
+ 3
You havent declare variable m and total, before the loop
9th Dec 2018, 6:32 AM
Taste
Taste - avatar
0
Oh sorry I typed in the wrong one
9th Dec 2018, 6:38 AM
Khk Gigi
Khk Gigi - avatar
0
z = 2 sum = 0 while z<9: z += 1 sum=sum+z print (sum)
9th Dec 2018, 6:40 AM
Khk Gigi
Khk Gigi - avatar
0
Whats wrong with that ?
9th Dec 2018, 6:44 AM
Taste
Taste - avatar
0
Nothing is, I just don’t know how you get the output
9th Dec 2018, 6:45 AM
Khk Gigi
Khk Gigi - avatar
0
Can you please explain it to me?
9th Dec 2018, 6:48 AM
Khk Gigi
Khk Gigi - avatar
0
Try adding this line print(str(sum)+"+"+str(z)) Inside the loop :D
9th Dec 2018, 6:53 AM
Taste
Taste - avatar
0
The answer is 42. I just wanna know how to get to it because I have this worksheet with a bunch of while loops problems but I don’t know how to do them without checking with python. Could you please show me the steps to it?
9th Dec 2018, 6:59 AM
Khk Gigi
Khk Gigi - avatar
0
z = 2 sum = 0 while z<9: print("z:",z,"\nsum:",sum,"\nz+=1: ", z+1, "\nsum+z: ",sum+(z+1),"\n----\n") z += 1 sum=sum+z print (sum)
9th Dec 2018, 7:08 AM
Taste
Taste - avatar
0
???? I don’t get it :/
9th Dec 2018, 7:08 AM
Khk Gigi
Khk Gigi - avatar
0
Try to run the code, each step will be printed
9th Dec 2018, 7:09 AM
Taste
Taste - avatar