Of Functions, Objects, and Methods-I NEED HELP PLEASE

Ethan Furman ethan at stoneleaf.us
Thu Jun 9 08:54:56 EDT 2011


Larry Hudson wrote:
> On 06/08/2011 01:09 PM, Cathy James wrote:
>> Dog Breed: "))
>>      while not dogs:
>>          print("Goodbye!!")
>>          sys.exit()
>>      else:
> 
> else does not belong with while.

else works just fine with while; it is the path taken when the while is 
exhausted, but not broken out of:

--> i = 5
--> while i:
...   print(i)
...   i -= 1
... else:
...   print("blast off!")
...
5
4
3
2
1
blast off!


--> i = 5
--> while i:
...   print(i)
...   i -= 1
...   if i == 3:
...     print('aborting')
...     break
... else:
...   print("blast off!")
...
5
4
aborting

~Ethan~



More information about the Python-list mailing list