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>Generally, when you don't know how many times you want to run your loop the solution is a <code>while</code> loop.   </p>

<pre><code>for seat in range(1,5):
   my_input = raw_input("Enter: ")
   while not(my_input == 'q' or isnumeric(my_input)):
        my_input = raw_imput("Please re-enter value")
   if my_input == 'q':
        break
   else:
       total += float(my_input)
</code></pre>

python