1

Can someone please help me with this

name=input("What is your name?").lower()
for char in name:
      if not char.isalpha():
        print ("invalid name")
        name=input("What is your name?").lower()

Unfortunately when I run it if i put the number 1 for name it will get it correct on the second try no matter what. and for example if i input 123 then the same process will repeat 3 times

What is your name?123
What is your name?123
invalid name
What is your name?bob
invalid name
What is your name?bob
invalid name
What is your name?bob
What is  9 - 8 ?

and if I enter 1

What is your name?1
invalid name
What is your name?bob
What is  7 - 4 ?
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
GamingKing720
  • 11
  • 1
  • 4
  • use `raw_input` instead of `input`. `input` *evaluates* your input: Input is quivalent to eval(raw_input(prompt)) – kmad1729 Oct 15 '15 at 17:59
  • @kmad1729 that is only true for Python 2.x . I am guessing OP is Python 3.x , since he gets a string back from input (otherwise he wouldn't be able to loop over it or check `isalpha()` ) . – Anand S Kumar Oct 15 '15 at 18:02
  • Ya, you are right. The OP seems to be using Python3. – kmad1729 Oct 15 '15 at 18:09

3 Answers3

3

The issue is that even if you found out that the name is invalid, the loop still continues on. Also, this only asks twice, and does not validate the name the second time.

Instead of only if, you should use a while loop and also, you do not actually need to check str.isalpha() for every character separately, you can check if for the string as a whole, it would return False, if any of the characters in the string are not alphabets. Example -

while True:
    name=input("What is your name?").lower()
    if name.isalpha():
        break
    print ("invalid name")

#Rest of your logic.

Examples for behavior of str.isalpha() on whole strings -

>>> 'asd'.isalpha()
True
>>> 'a1sd'.isalpha()
False
>>> 'a_sd'.isalpha()
False
>>> 'aAdsd'.isalpha()
True
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

First if you wish to return a string (rather than have Python interpret your input as an expression), change input() to raw_input() (handy explanation available here). This only applies for Python 2.X rather than 3.X.

Second, isalpha() from the documentation:

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

Therefore you would be able to pass in your entire string rather than checking every character.

It is going to loop over three times because you are looping over the initial string (e.g. '123'), if you were to input '1bc' the first try would be invalid however the first and second would be valid.

Instead, why not just check whether the name contains letters only as a whole?

name = raw_input('What is your name?').lower()
valid = False

while not valid:
    if name.isalpha():
        is_valid = True
        # Do whatever you need to do
    else:
        name = raw_input("Name is invalid, please try again.").lower()

Or if you wish you use the while block Anand S Kumar wrote out if it's suits you best (I just prefer it being a bit more explicit).

Community
  • 1
  • 1
Juxhin
  • 5,068
  • 8
  • 29
  • 55
0

The main problem is because when you use for char in name:, it is looping through all the previously wrong inputs (in your case: 123). Hence, it will ask you to input your name for 3 more times.

In order not to deviate too much from what you have done, i have come up with this for your reference.Do take note that isalpha() can be used on name itself, so you no need to loop through all the characters.

name = input("What is your name?").lower()
while not name.isalpha():
    print("invalid name")
    name = input("What is your name?").lower()

print('Your name is ' + name)
John Jam
  • 185
  • 1
  • 1
  • 17