0

I'm fairly new to python, and I've started creating a little script that saves a user's name into a text file. I want to have a list of blacklisted characters that can't appear in one's name, but I only manage to not let the blacklisted characters BE the name. How can I do this?

Name = input("What is your name?\n")
NameDoc = open("Name.txt", "w")



Black_List = ["B", "A"]

if Name in Black_List:
    print("Blacklisted Name")
elif len(Name) > 10:
    print("Sorry, 10 characters max!")
else:
    NameDoc.write(Name)
    NameDoc.close()
    Name2 = open("Name.txt", "r")
    print("Name accepted!")
    print("Hello " + Name2.read())
    Name2.close()

try:
    NameDoc.close()
except:
    pass
esqew
  • 42,425
  • 27
  • 92
  • 132

2 Answers2

3
if Name in Black_List:

This checks if the entire name is in the black list. Instead you want to check each char from the name separately:

if any(ch in Black_List for ch in Name):
nosklo
  • 217,122
  • 57
  • 293
  • 297
0

i created an example code here, blackc stands for blacklisted characters

blackc = ['a','n']

name = input('what is your name?: ' )

for character in blackc:
    if character in name:
        print('selct a new name')
        break
Stanley
  • 2,434
  • 18
  • 28