Force raw_input

How to implement the following:

title_selection = raw_input("Please type in the number of your title and press Enter.\n%s" % (raw_input_string))
if not title:
    # repeat raw_input
+3
source share
2 answers

This is often done using the loop-and-half construction with breakin the middle:

while True:
    title_selection = raw_input("Please type in the number of your title and press Enter.\n%s" % (raw_input_string))
    if title_selection:
        break
    print "Sorry, you have to enter something."

This method gives you the option to print a message about what the program expects.

+11
source
title_selection = ''
while not title_selection:
  title_selection = raw_input("Please type in the number of your title and press Enter.\n%s" % (raw_input_string))

It must be defined title_selectionas ''before hand, which means empty (also False). notwill make Falseequal True (negation).

+12
source

All Articles