Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is @Ashwini Chaudhary's answer, slightly changed.</p> <pre><code>question = "You come to a crossroads. Go left or right?" lor = raw_input(question) while not "left".startswith(lor) and not "right".startswith(lor): print "That's not a direction." lor = raw_input(question) if "left".startswith(lor): print "You went left" else: print "You went right" </code></pre> <p>This change allows the user to type less than the whole word. The user can go left by typing "l", "le", "lef", or "left". Similarly the user can go right by typing "r", "ri", "rig", etc.</p> <p>Also, since the question was duplicated I made a variable and used the variable twice. That way if you change the question, you only need to change it in one place.</p> <p>You could write a function that takes a question and a list of valid answers to the question, and then asks the question over and over in a loop until a valid answer was given. Then the function would return the valid answer. Also, the question can always return the full answer even if the user entered an abbreviation. Here's code to do that:</p> <pre><code>def ask(question, lst_answers): if not question.endswith(' '): question += ' ' while True: answer = raw_input(question).strip().lower() lst = [a for a in lst_answers if a.startswith(answer)] if len(lst) == 1: return lst[0] else: print "Please answer with one of: " + ' '.join(lst_answers) answer = ask("You come to a crossroads. Go left or right?", ["left", "right"]) if answer == "left": print "You went left" else: print "You went right" </code></pre> <p>This may look tricky, but actually this is the lazy way to do it. We solved the basic problem in one place, and then your code can just call this function and rely on it to do the right thing.</p> <p>The heart of the <code>ask()</code> function is a "list comprehension", where we make a list of all the answers that match an expression. The expression is <code>a.startswith(answer)</code>, so we get a new list with all the answers that start with what the user typed. Then we check to see if we got exactly one answer. If we got zero answers, the user typed something weird. If we got more than one answer, the user typed a short abbreviation but multiple words matched: for example if we accept answers of "left" or "look" and the user just typed "l". If we got exactly one answer, we return that answer, which happily enough is the full answer and not the abbreviation that the user typed!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload