user888469
user888469

Reputation: 137

Python validation loop

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")

Upvotes: 0

Views: 6966

Answers (5)

adder
adder

Reputation: 3698

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')

Upvotes: 0

Chen A.
Chen A.

Reputation: 11280

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)

Upvotes: 0

Kyle Higginson
Kyle Higginson

Reputation: 942

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

Upvotes: 0

Kieron
Kieron

Reputation: 21

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")

Upvotes: 0

Dooley
Dooley

Reputation: 21

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()

Upvotes: 1

Related Questions