Home > database >  How do i put "denied" characters in the same line?
How do i put "denied" characters in the same line?

Time:06-14

Summary: i wanted to clean up my messy code by putting all the "denied" characters in the same line, and.. I kinda don't know how to, i have tried using the "or" keyword, i also tried the "elif" keyword, but both changed nothing, heres the code: (the ? And ! Are what i want denied)

username=input("input username= ")
if "?" in username:
    print("the following characters are         allowed: a-z, -, num's")
    quit()
if "!" in username:
    print("only a-z, -, and num's are                 allowed")
    quit()
else:
    print("hello", username)

CodePudding user response:

You can group the characters you want ignored in a list then use python's 'any' statment to check if any of the characters to be denied are found in username as follows :

username=input("input username= ")

# put unwanted chars here
to_deny = ['?', '!']

if any([char in username for char in to_deny]):
    print("only a-z, -, and num's are allowed")
    quit()
else:
    print("hello", username)

CodePudding user response:

You might use set arithemtic to detect if string contain at least one of denied characters as follows

username=input("input username= ")
denied = {"?","!"}
if denied.intersection(username):
    print("username contain denied character or character, denied are:",*denied)
else:
    print("hello", username)
  • Related