-4

Pretty new to Python and am using 2.7 to script.

a = raw_input("Input number or decimal only")
Try:
    Y = float(a)
    print a
except ValueError:
    print "wrong input, try again"

I need to loop this - if the input is either alphanumeric or letters, it should ask for input again.

I can't seem to find a way to slide in a while loop in it.

(Python newbie, please be kind!)

pppery
  • 3,731
  • 22
  • 33
  • 46
Doran
  • 9
  • 1

3 Answers3

0
correct = False
a = ""
while not correct:
    a = raw_input("Input number or decimal only")
    correct = a.isalnum()
    if not correct:
        print "wrong input, try again"
print a
Flying_Banana
  • 2,864
  • 2
  • 22
  • 38
0
input = "%"    # Set to be non alphanumeric so loop starts
while not input.isalnum():
    input = raw_input("Input number or decimal only > ")
    if not input.isalnum():
        print ("Invalid Input, try again")
    else:
        print (input)
Tim
  • 2,563
  • 1
  • 23
  • 31
0
while True: # Loops!
    a = raw_input("Input number or decimal only\n")
    try:
        Y = float(a)
        print a
        break # Interrupts the loop if the input was a number
    except ValueError:
        print "wrong input, try again"

'while True' loops forever, the only way to stop it is 'break' within the loop