0

I need to validate the user's name so it only contains letters. For this i am using a while true statement. I also want the username to be written to a text file once it is valid how would i change my code to do this. How would i change my code so if the user enters an invalid name they have to try again, and if it is valid it is written to a text file.

import re
name=(input("Please enter your name: "))
while name is None or not re.match("[A-z]",name):
  print("Invalid name. Try again")
  else:
    filename = ("name");
    with open (filename, "a") as f:
      f.write (name + "\n")
user888469
  • 137
  • 2
  • 8
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Sep 24 '17 at 20:00

5 Answers5

1
import re

def askName():
    name = input("Please enter your name: ")
    if re.match("[A-z]",name):
        with open("filename", "w") as f:
            f.write(name)
    else:
        askName()     

askName()
Dooley
  • 21
  • 4
0

From the documention:

str.isalpha()

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard.

Something along the following lines should do the trick:

name = input('Enter name: ')

# if you want to take spaces into account, i.e. inputs like 
# 'Spam Eggs', change the following line to: 
# while any(x for x in name.split() if not x.isalpha()):

while not name.isalpha():
    print('Invalid input!')
    name = input('Enter name: ')

with open(filename, 'a') as f:
    f.write(name + '\n')
adder
  • 3,512
  • 1
  • 16
  • 28
0
name = raw_input('enter name\n')

while not all(map(lambda l: l.isalpha, iter(name)):
           name = raw_input('invalid name. Please try again\n')
           continue

with open('file_path', 'w') as f:
           f.write(name + '\n')

all() statement validates every char in the string; the map runs the lambda function for every char in the input. To create an iterator out of a string, you can use iter(name)

Chen A.
  • 10,140
  • 3
  • 42
  • 61
0

Hope this helps

Correct = True
while Correct == True:
    name = input("Please enter your name: ")
    if name.isalpha() == True:
        file = open("filename.txt", "a")
        file.write("\n" + name)
        file.close
        print("Wrote to file")
        Correct = False
    else:
        print("Incorrect, Try again")
Kieron
  • 21
  • 7
0

My solution upon many others:

while True:
    name=(input("Please enter your name: "))
    if name and name.isalpha():
        filename = ("name.txt")
        with open (filename, "a") as f:
            f.write (name + "\n")
        break
    else:
        print ("Invalid name. Try again")
        continue
Kyle Higginson
  • 922
  • 6
  • 11