试图让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")
Run Code Online (Sandbox Code Playgroud)
我一直在尝试please,它出现了,因为Only alphabetical letters and spaces: no我用or而不是and,但只满足一个条件.我需要满足两个条件.那句话必须只包含字母和空格,但必须至少包含一种.它不得包含任何数字.
我在这里错过了什么让python返回字母和空格只包含在字符串中?
一旦异常消失,我想要一个if语句.它的功能是什么,或者甚至是可能的?
这就是我想要的
try:
save_this_number_file(s)
except:
print("could not find file. Don't forget to add *.txt* to the end of file name.")
if (except):
again = input("Would you like to try again? (y/n) ")
if (again != 'y'):
sys.exit()
Run Code Online (Sandbox Code Playgroud)