如何进行简单的长度验证

时间:2019-03-13 19:36:52

标签: python

ammountofpassword = int(input("Please input the number of usernames needed"))

for counter in range (0,ammountofpassword):

    password = str(input("Input the 3 letter password"))

    length = len(password)

    while  length > 3 and length < 3:
        print("please enter a 3 letter password")
        name = str(input("Please input the 3 letter password"))

    print("your password is",password)

2 个答案:

答案 0 :(得分:0)

似乎您想让用户输入3个字符的密码。您尝试过这种方法吗?

ammountofpassword = int(input("Please input the number of usernames needed"))

for counter in range (0,ammountofpassword):

    password = str(input("Input the 3 letter password"))

    length = len(password)

    while  length != 3:
        print("please enter a 3 letter password")
        password = str(input("Please input the 3 letter password"))
        length = len(password)

    print("your password is",password)


答案 1 :(得分:0)

以下情况不正确

df = df.persist()

因为while length > 3 and length < 3: 变量的值永远不会大于和小于length ,所以3循环将永远不会运行。

您似乎实际上需要使用while运算符

or

如果while length > 3 or length < 3: 大于或小于True,则为length

进一步,我想使用3运算符

会更好
!=

表示“长度不是3”。