+ 1
i = 1 while i <=5: print(i) i = i + 1 print("Finished!")
1 finished 2 finished 3 finished....???why
3 odpowiedzi
+ 1
In above program,
step 1.initialize the variable i value as 1.
step 2. in while loop check for the condition (i<=5)
step 3.if condition is true,
step 3.1 print current value of i variable by 1.
step 3.2 increment the value of i
step 3.3 print the "Finished" string
step 4. check for the condition (i<=5) and program continuous execution from 3 till the condition is false.
step 5. program exits
+ 1
I think you wanted to print "finished" only at the end. In that case, make sure that the "print("Finished")" statement is NOT indented.
If it is indented, it is part of the while loop, and it will be executed every single time the loop runs.
Try this:
i = 1
while i <=5:
print(i)
i = i + 1
print("Finished!")
And next time, please don't post your code in the title - it ruins the indentation, and Python relies very much on that :)
0
I'm not sure what you are asking for to be honest.
Every time it loops, it'll print the current value of the variable 'i', add 1 to the variable, then print finished. This continues until the variable reaches 5. This is because the while loop condition remains true as long as the variable is less than OR equal to the value 5.