1

I'm a beginner in the Python language. Is there a "try and except" function in python to check if the input is a LETTER or multiple LETTERS. If it isn't, ask for an input again? (I made one in which you have to enter an integer number)

def validation(i):
try:
    result = int(i)
    return(result)
except ValueError:
    print("Please enter a number")

def start():
x = input("Enter Number: ")
z = validation(x)
if z != None:

    #Rest of function code
    print("Success")
else:
    start()
start()

When the above code is executed, and an integer number is entered, you get this:

Enter Number: 1
 Success

If and invalid value however, such as a letter or floating point number is entered, you get this:

Enter Number: Hello
Please enter a number
Enter Number: 4.6
Please enter a number
Enter Number: 

As you can see it will keep looping until a valid NUMBER value is entered. So is it possible to use the "try and except" function to keep looping until a letter is entered? To make it clearer, I'll explain in vague structured English, not pseudo code, but just to help make it clearer:

print ("Hello this will calculate your lucky number")

# Note this isn't the whole program, its just the validation section.
 input (lucky number)
# English on what I want the code to do: 

    x = input (luckynumber)

So what I want is that if the variable "x" IS NOT a letter, or multiple letters, it should repeat this input (x) until the user enters a valid letter or multiple letters. In other words, if a letter(s) isn't entered, the program will not continue until the input is a letter(s). I hope this makes it clearer.

Cœur
  • 37,241
  • 25
  • 195
  • 267
June
  • 11
  • 3
  • 1
    All inputs are valid strings. When would you want an input to be rejected? – jwodder Nov 30 '15 at 17:34
  • Yes it is possible. But what are you trying to do? – Damian Chrzanowski Nov 30 '15 at 17:36
  • @jwodder The function should make sure that the input entered is a letter(s) before continuing, if not, then the question should be looped until the user enters a letter or multiple letters. Sorry if I wasn't clear enough. – June Dec 03 '15 at 09:59

2 Answers2

2

You can just call the same function again, in the try/except clause - to do that, you'll have to adjust your logic a bit:

def validate_integer():
   x = input('Please enter a number: ')
   try:
      int(x)
   except ValueError:
      print('Sorry, {} is not a valid number'.format(x))
      return validate_integer()
   return x

def start():
   x = validate_integer()
   if x:
      print('Success!')
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I need to do the opposite, meaning that the function should validate that the input is a letter(s) before continuing. So if an int or float value is entered, it should keep repeating the question (for example "Sorry, { } isn't a valid letter). Apologies if the question wasn't clear enough. – June Dec 03 '15 at 09:56
0

Don't use recursion in Python when simple iteration will do.

def validate(i):
    try:
        result = int(i)
        return result
    except ValueError:
        pass

def start():
    z = None
    while z is None:
        x = input("Please enter a number: ")
        z = validate(x)

    print("Success")

start()
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 3
    While a valid and correct remark, this does not answer the question, since it addresses the int case only. (Not that the question is crystal clear though...) – Thomas Nov 30 '15 at 17:39