小编sit*_*lia的帖子

检查字符串是否只是字母和空格 - Python

试图让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返回字母和空格只包含在字符串中?

python contain

6
推荐指数
1
解决办法
2万
查看次数

Python - 发生异常时的"if"语句

一旦异常消失,我想要一个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)

python if-statement exception

-1
推荐指数
1
解决办法
124
查看次数

标签 统计

python ×2

contain ×1

exception ×1

if-statement ×1