2
fn = input("Hello, what is your first name?")
firstname = (fn[0].upper())
ln = input("Hello, what is your last name?")
lastname = (ln.lower())

I want fn to be on a loop so that if they enter their a number instead of letters, it would repeat the question

  • You don't give an indication of what "correct" means in the context of the query? – Andrew Sep 12 '16 at 19:15
  • 1
    start reading: https://wiki.python.org/moin/WhileLoop https://docs.python.org/2/tutorial/controlflow.html – Marc B Sep 12 '16 at 19:16
  • What don't you understand? You're looking for basic documentation on loops. – SLaks Sep 12 '16 at 19:16
  • Adding to @MarcB - Once done with WHILE, then check this - http://stackoverflow.com/questions/5424716/python-how-to-check-if-input-is-a-number – Dinesh Pundkar Sep 12 '16 at 19:19
  • Write a function to get the input. Write a function to validate the input (and give a reason if it rejects it). Write a loop which calls those functions. It's a little odd in python because you have to check at the end, but that is easy to handle: http://stackoverflow.com/questions/743164/emulate-a-do-while-loop-in-python – Kenny Ostrom Sep 12 '16 at 19:22
  • Also - fn[0].upper() - not required. There is title method. Check this - http://www.tutorialspoint.com/python/string_title.htm – Dinesh Pundkar Sep 12 '16 at 19:24
  • Thanks for all of these links, will definitely read them. Didn't think about using a function that will definitely try that @KennyOstrom – Krish Mehta Sep 12 '16 at 19:28

2 Answers2

3

I guess you need something like this

final_fn = ""
while True:
    fn = input("Hello, what is your first name?")
    if valid(fn):
        final_fn = fn
        break

Define you validation method before it. An example would be as Joran mentioned

def valid(fn):
    return fn.isalpha()
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
rafaelc
  • 57,686
  • 15
  • 58
  • 82
  • 1
    good job making it more abstract and relevant to many classifications of input validation instead of just what OP asked for +1 from me – Joran Beasley Sep 12 '16 at 19:23
  • Simpler: Don't have separate `final_fn` and `fn`. You won't leave the loop until you have a valid value anyway, so the separate names are pointless. In fact, if the empty string is invalid (or you have some known invalid value to use) you could just do: `fn = ''`, then `while not valid(fn):` fn = input(...)` simplifying the five line loop down to two lines. – ShadowRanger Sep 13 '16 at 01:24
1
if result.isalpha():
   print "the string entered contains only letters !"

I guess ?

a="6"
while not a.isalpha():
    a = raw_input("Enter your name:")
print "You entered:",a

if you just wanted to eliminate only words that contained numbers you could do

while any(ltr.isdigit() for ltr in a):
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • This will reject input with numbers in, but will also reject input with hyphens in. Bad news for people with hyphens in their names. – Steve Jessop Sep 12 '16 at 19:22