0

Trying to get python to return that a string contains ONLY alphabetic letters AND not spaces .

def valide(name):

for i in range(0,len(name)):
    if name[i] == " ":
        print("error, no spaces allowed!")
        break
while True:
    nam = input("give us the name: ")
    valide(nam)

It's like an login system that does not allow the name to have sapce here i want if the string contains a space then return to getting another name and rechecking the condition.

i want the user re-enter another name to re-check if it contains a space

  • Ok, what's wrong with your code? – Austin May 30 '21 at 09:54
  • Does this answer your question? [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – CoolCoder May 30 '21 at 09:56
  • 2
    Does this answer your question? [Check if space is in a string](https://stackoverflow.com/questions/3301395/check-if-space-is-in-a-string) – Tabaraei May 30 '21 at 09:57
  • @Austin i want the user re-enter another name to re-check if it contains a space – Adib Akkari May 30 '21 at 10:12
  • @AdibAkkari You have a `while True` block, so it will ask for another one. – Austin May 30 '21 at 10:18

2 Answers2

0

A simpler solution is to use the statement (' ' in nam) == True will return True if the string contains a space.

Hence, the code is simplified to:

while True:
    nam = input("give us the name: ")
    if (' ' in nam):
        print("error, no space allowed!")
Ashok Arora
  • 531
  • 1
  • 6
  • 17
0
while True:
    nam = input("give us the name: ")
    if ' ' in nam:
        print("error, no space allowed!")
    else:
        print("no spaced detected")
Tabaraei
  • 445
  • 2
  • 7
  • 18