简体   繁体   中英

python 3.X if statements

I am trying to write a code that asks from the user to get input that equal to only one word (az)- my requirements :

If the user entered a string of letters that has more than one character, print the string "E1" on the screen.

If the user entered a character that is not an English letter (for example, a sign such as: &, *), print the string "E2" on the screen.

If the user entered a string of letters that contains more than one death and also contains characters that are not letters in English, print the string "E3" on the screen

I am trying this code :

Word = input(('Please choice word:\n'))
if Word == 'a''b':
    print("good")
else:
    print("not good")

but is seems me that the code is too long and clumsy.

Word = input(('Please choice word:\n'))

specialChars = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

if(Word in specialChars ):
    print("E2")
    
else:
    for char in Word:
        if(char in specialChars):
            print("E3")
            break
    print("E1")

I think you are looking for this. I still don't know what more than one death means.

wd = input ('Please choice word:\n')

if len(wd) > 1: #first check if the length of the word is one character
    if not (all((a.isalpha() or a ==' ') for a in wd)):
        print ('E3') #if more than one char and if any of them are special chars, then E3
    else:
        print ('E1') #if more than one char but word is ([a-z] or [A-Z])
elif not wd.isalpha(): #check if letter is [a-z] or [A-Z]. if not, then E2
    print ('E2')

The output for this will be:

Please choice word:
Good
E1
Please choice word:
B@d
E3
Please choice word:
@
E2
Please choice word:
Good Day
E1
Please choice word:
B

The last one does not print anything in response as it meets all the criteria.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM