+ 1

I really didn't get why a "while" needs an "else"??

Else after while

28th Mar 2017, 5:08 PM
SAMI Awad
SAMI Awad - avatar
5 Answers
+ 6
Much like superdarkec wrote, the while loop allows else statement. The statement within else gets executed only if the while condition gets False OR is False from the very beginning. Try removing the whole if clause from the cide you submitted and see how it works then. Bear in mind that 'else' is actually an integral part of the 'while' loop and using 'break' escapes the whole statement ('else' IS NOT executed then).
28th Mar 2017, 8:04 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 2
Python allows for Else statements in while loops but they are by no means neccessary. You don't need one to write a functional while loop. But you can use one and it gets executed when the condition of the while loop becomes false.
28th Mar 2017, 5:49 PM
superdarkec
superdarkec - avatar
+ 1
x=10 while x>0: print(x) x-=1 if x==2: break else: print("done") print("finished")
28th Mar 2017, 5:31 PM
SAMI Awad
SAMI Awad - avatar
+ 1
'else' some Times do'snt obligation in 'while' fonction
1st Apr 2017, 12:11 PM
mohamed
mohamed - avatar
0
The else belongs to the while
28th Mar 2017, 5:43 PM
SAMI Awad
SAMI Awad - avatar