python 3.X if statements



我正在尝试编写一个代码,要求用户获得仅等于一个单词(a-z(的输入-我的要求:

如果用户输入了具有一个以上字符的字符串;E1〃;在屏幕上。

如果用户输入了一个不是英文字母的字符(例如:&,*等符号(,则打印字符串";E2";在屏幕上。

如果用户输入了一个包含一个以上死亡并且还包含不是英文字母的字符的字符串;E3";在屏幕上

我正在尝试这个代码:

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

但我觉得代码太长太笨拙了。

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

我想你正在寻找这个。我仍然不知道more than one death是什么意思。

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

其输出为:

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

最后一个不打印任何响应,因为它符合所有标准。

最新更新