试图让python返回字符串只包含字母和空格。
string = input("Enter a string: ")
if all(x.isalpha() and x.isspace() for x in string):
print("Only alphabetical letters and spaces: yes")
else:
print("Only alphabetical letters and spaces: no")我一直在尝试please,作为Only alphabetical letters and spaces: no,我使用了or而不是and,但这只满足了一个条件。我需要满足这两个条件。也就是说,句子必须只包含字母和空格,但必须至少有一种类型。它不能包含任何数字。
对于python返回字母和空格只包含在字符串中,我在这里遗漏了什么?
发布于 2015-04-05 17:57:51
字符不能同时是alpha 和(空格)。它可以是一个alpha 或一个空格。
要求字符串仅包含alpha和空格:
string = input("Enter a string: ")
if all(x.isalpha() or x.isspace() for x in string):
print("Only alphabetical letters and spaces: yes")
else:
print("Only alphabetical letters and spaces: no")要求字符串至少包含一个alpha和至少一个空格:
if any(x.isalpha() for x in string) and any(x.isspace() for x in string):若要要求字符串至少包含一个alpha、至少一个空格,并且只包含alpha和空格,请执行以下操作:
if (any(x.isalpha() for x in string)
and any(x.isspace() for x in string)
and all(x.isalpha() or x.isspace() for x in string)):测试:
>>> string = "PLEASE"
>>> if (any(x.isalpha() for x in string)
... and any(x.isspace() for x in string)
... and all(x.isalpha() or x.isspace() for x in string)):
... print "match"
... else:
... print "no match"
...
no match
>>> string = "PLEASE "
>>> if (any(x.isalpha() for x in string)
... and any(x.isspace() for x in string)
... and all(x.isalpha() or x.isspace() for x in string)):
... print "match"
... else:
... print "no match"
...
match发布于 2015-04-05 18:01:02
正确的解决方案将使用or。
string = input("Enter a string: ")
if all(x.isalpha() or x.isspace() for x in string):
print("Only alphabetical letters and spaces: yes")
else:
print("Only alphabetical letters and spaces: no")虽然您有一个字符串,但您正在迭代该字符串的字母,因此每次只有一个字母。因此,一个字符本身不可能是一个字母字符和一个空格,但它只需要是其中之一,以满足您的约束。
编辑:我在另一个回答中看到了你的评论。alphabet = string.isalpha()返回True,当且仅当字符串中的所有字符都是字母。这不是您想要的,因为您说希望您的代码在使用字符串yes执行时打印please,该字符串有一个空格。您需要单独检查每个字母,而不是整个字符串。
为了让您确信代码确实是正确的(好的,您需要自己执行它才能被说服,但无论如何):
>>> string = "please "
>>> if all(x.isalpha() or x.isspace() for x in string):
print("Only alphabetical letters and spaces: yes")
else:
print("Only alphabetical letters and spaces: no")
Only alphabetical letters and spaces: yes编辑2:从您的新评论来看,需要这样的内容:
def hasSpaceAndAlpha(string):
return any(char.isalpha() for char in string) and any(char.isspace() for char in string) and all(char.isalpha() or char.isspace() for char in string)
>>> hasSpaceAndAlpha("text# ")
False
>>> hasSpaceAndAlpha("text")
False
>>> hasSpaceAndAlpha("text ")
True或
def hasSpaceAndAlpha(string):
if any(char.isalpha() for char in string) and any(char.isspace() for char in string) and all(char.isalpha() or char.isspace() for char in string):
print("Only alphabetical letters and spaces: yes")
else:
print("Only alphabetical letters and spaces: no")
>>> hasSpaceAndAlpha("text# ")
Only alphabetical letters and spaces: no
>>> hasSpaceAndAlpha("text")
Only alphabetical letters and spaces: no
>>> hasSpaceAndAlpha("text ")
Only alphabetical letters and spaces: yes发布于 2015-04-05 18:55:04
实际上,这是一个模式匹配练习,那么为什么不使用模式匹配呢?
import re
r = re.compile("^[a-zA-Z ]*$")
def test(s):
return not r.match(s) is None 或者在解决方案中是否需要使用any()?
https://stackoverflow.com/questions/29460405
复制相似问题