[Tutor] One of my 'baby step' programs

Kent Johnson kent37 at tds.net
Mon Oct 2 12:03:43 CEST 2006


Luke Paireepinart wrote:
> Alan Gilfoy wrote:
>> -code block-
>>
>> number = 3
>> running = True
>>
>> while running:
>>    guess = int(raw_input("Please enter a number : ")) #lets user guess a number
>>
>>    if guess == number:
>>        print "Yay, you got the right number, good for you. But you 
>> don't get any prizes. Do I look like a walking ATM to you?"
>>        running = False #this causes the guess-again loop to start.
>>    elif guess < number:
>>        print "No, my number is higher than that"
>>    else:
>>        print "No, my number is lower than that."
>>
>> else:
>>    print "The number-guessing loop is over."
>>   
> I had no idea you could have an 'else' tied to a 'while' loop.
> Interesting....

It's useful but in this case not necessary - since there is no 'break' 
within the loop, the 'else' clause will always be executed. That is 
correct behaviour in this case, so it would be better to leave out the 
else and just have the print.

Kent



More information about the Tutor mailing list