执行第三个循环时输入不检查第一个循环? py3

Input does not check for first loop when 3rd loop is executed? py3

我正在 python3 中输入用户名。我只对字符串使用了 3 个 while 循环,限制 12 个字符且少于 3 个字符。所以名字必须是字母和真实的名字。当我 运行 编程并输入错误的名称时,它一直要求更正名称,但是当我给出第三个循环的直接正确输入时,它不会检查前两个循环。我的意思是,当我输入连续输入超过 12 个字符并突然输入带有数字的随机名称时,它只接受输入但不执行第一个循环,程序只是在终端 window 结束。 这是我的代码:

print('lets see what it does')

f_name=input('Enter your first name:')
while f_name.isalpha()==False:
    print('Please Enter Alphabets only')
    f_name=input('Please reenter name:')
while len(f_name)>12:
    print('Name exceeds character limit.')
    f_name=input('Please reenter name:')
while len(f_name)<3:
    print('Name must be atlest three characters long')
    f_name=input('Please ReEnter Name:')

这是输出:

lets see what it does
Enter your first name:ppp000
Please Enter Alphabets only
Please reenter name:pppppppppppppppp8
Please Enter Alphabets only
Please reenter name:ppppppppppppppp8
Please Enter Alphabets only
Please reenter name:pp9
Please Enter Alphabets only
Please reenter name:pp00
Please Enter Alphabets only
Please reenter name:pppppppppp8
Please Enter Alphabets only
Please reenter name:jijija8
Please Enter Alphabets only
Please reenter name:ijajsidaosdasdasdasd
Name exceeds character limit.
Please reenter name:ppp0```

我建议使用函数来验证名称。然后你可以使用 := 运算符来定义 f_name.

def is_name_valid(name):
    return (
            name.isalpha() and
            12 >= len(name) >= 3
    )


def main():
    while not is_name_valid(f_name := input('Enter your first name: ')):
        print("Name is invalid")
    print(f_name)

如果需要提示为什么名字无效,可以使用不同的return值:0 for valid, 1 用于 .isalpha() 等...为了更好地处理错误,您可能需要使用 enumsconsts.