Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何检查输入是否仅为字符串或不在或特殊字符中的字符?_Python 3.x - Fatal编程技术网

Python 3.x 如何检查输入是否仅为字符串或不在或特殊字符中的字符?

Python 3.x 如何检查输入是否仅为字符串或不在或特殊字符中的字符?,python-3.x,Python 3.x,和年龄变量一样,我们必须严格输入数字,因为int()是唯一的原因。那么,还有什么方法来命名呢。只有字母表,没有任何数字或字符串 结论应该是: 如果输入的名称中输入了数字或特殊字符,则应显示错误。请查看以下两个标准的str函数,它们可能就是您要查找的: :true如果str仅包含字母(至少一个) :true如果str仅包含数字(至少一个) 你可以这样使用它们: students = [] while True: Ask = input("Do you Want to Add Stud

和年龄变量一样,我们必须严格输入数字,因为int()是唯一的原因。那么,还有什么方法来命名呢。只有字母表,没有任何数字或字符串

结论应该是:


如果输入的名称中输入了数字或特殊字符,则应显示错误。

请查看以下两个标准的
str
函数,它们可能就是您要查找的:

  • true
    如果
    str
    仅包含字母(至少一个)
  • true
    如果
    str
    仅包含数字(至少一个)
你可以这样使用它们:

students = []

while True:
    Ask = input("Do you Want to Add Student:").lower()
    try:
        if Ask == "Yes".lower():
            student_name = input("Enter Student Name:")
            student_id = int(input("Enter Student ID:"))


            def get_students_titlecase():
                students_titlecase = []
                for student in students:
                    students_titlecase = student["name"].title()
                return students_titlecase


            def print_students_titlecase():
                students_titlecase = get_students_titlecase()
                print(students_titlecase)


            def add_student(name, student_id):
                student = {"name": name, "student_id": student_id}
                students.append(student)

            Ask = input("Do you Want to Add Student:").lower()
            if Ask == "Yes".lower():
                student_list = get_students_titlecase()
                add_student(student_name, student_id)
                print_students_titlecase()
                print(students)
            else:
                continue
        else:
            print("Type Yes if you To Enter The Student Cred".center(50))
    except Exception:
        print("Write Valid Values")
`

它的代码要长一点。等等,我在上面添加了我的示例代码。
def input_name():
    while True:
        name = input("Enter your name: ")

        if name.isalpha():
            return name

        print("Please only use letters: [a-zA-Z]. Try again.")


def input_age():
    while True:
        age = input("Enter your age: ")

        if age.isdigit():
            return int(age)

        print("Please only use digits: [0-9]. Try again.")


name = input_name()
age = input_age()