[Tutor] (no subject)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Aug 17 17:46:39 CEST 2006



>> > Mr. Yoo im working on a program to write a sudoku puzzle and if
>> > something is wrong with the last row i need to re make the whole puzzle
>> > over again.

>> Here's a separate question that's related to the first: can you write a
>> function that asks the user to enter a word, and continues prompting until
>> the user enters a non-empty word?

Hi Amadeo,


> Yes I can Mr. Yoo, but i dont really want to make my whole program a 
> loop.

Ok.  Why not?  I'm curious: is there a particular constraint you're trying 
to work under, or is it something else?

Also, I'm not sure what you mean by "whole program".


In any case, here's a way to do it without an explicit 'for' or 'while' 
loop:

####################################
def ask_for_input():
     msg = raw_input("enter a word:")
     if msg:
         return msg
     else:
         return ask_for_input()
####################################

This would not be the idiomatic way to do this in Python only because of a 
technical limitation in the main Python implementation (it doesn't support 
"tail call optimization").

Still, would this be more acceptable to you, or do you also consider this 
a "loop"?

(Personally, I do. *grin*)


More information about the Tutor mailing list