How to give a warning and ask for raw_input again - while inside a loop
<p>I have written the Python code below (actually it's my solution for an exercise from page 80 of "Teach yourself Python in 24 hours").</p>

<p>The idea is: there are 4 seats around the table, the waiter knows for how much each seat ordered, enters those 4 amounts and gets a total.</p>

<p>If the raw_input provided is not a number (but a string) my code kicks the person out. The goal, however, is to give an error message ("this entry is not valid") and ask for the input again - until it's numeric. However, I can't figure out how to ask the user for the raw input again - because I am already inside a loop.</p>

<p>Thanks a lot for your advice!</p>

<pre><code>def is_numeric(value):
  try:
    input = float(value)
  except ValueError:
    return False
  else:
    return True


total = 0
for seat in range(1,5):

  print 'Note the amount for seat', seat, 'and'
  myinput = raw_input("enter it here ['q' to quit]: ")

  if myinput == 'q':
    break

  elif is_numeric(myinput):
    floatinput = float(myinput)
    total = total + floatinput

  else:
    print 'I\'m sorry, but {} isn\'t valid. Please try again'.format(myinput)
    break

if myinput == 'q':
  print "Goodbye!"
else:
  total = round(total, 2)
  print "*****\nTotal: ${}".format(total)
  print "Goodbye!"
</code></pre>

<p>As Patrick Haugh and SilentLupin suggested, a <code>while</code> loop is probably the best way. Another way is recursion- ie, calling the same function over and over until you get a valid input: </p>

<pre><code>def is_numeric(value):
  try:
    input = float(value)
  except ValueError:
    return False
  else:
    return True

def is_q(value):
  return value ==  'q'

def is_valid(value, validators):
  return any(validator(input) for validator in validators) 

def get_valid_input(msg, validators):
  value = raw_input(msg)
  if not is_valid(value, validators):
    print 'I\'m sorry, but {} isn\'t valid. Please try again'.format(value)
    value = get_valid_input(msg, validators)
  return value

total = 0
for seat in range(1,5):

  print 'Note the amount for seat', seat, 'and'
  myinput = get_valid_input("enter it here ['q' to quit]: ", [is_q, is_numeric])

  if myinput == 'q':
    break

  elif is_numeric(myinput):
    floatinput = float(myinput)
    total = total + floatinput

if myinput == 'q':
  print "Goodbye!"
else:
  total = round(total, 2)
  print "*****\nTotal: ${}".format(total)
  print "Goodbye!"
</code></pre>

<p>In the above code, <code>get_valid_input</code> calls itself over and over again until one of the supplied <code>validators</code> produces something truthy. </p>

python