Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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中的所有字符或数字_Python_Pycharm - Fatal编程技术网

如何知道用户输入是否包含python中的所有字符或数字

如何知道用户输入是否包含python中的所有字符或数字,python,pycharm,Python,Pycharm,我正在使用PyCharm,希望知道如何设置字符串/变量来告诉我用户输入的是字母字符还是数字?下面是一个伪代码示例: weak = all characters or numbers strong = a mix of characters and numbers INPUT password 在这种情况下,“isaplpha”或“isnumeric”是否能派上用场?如果是,怎么做?是的!你是对的,isalpha和isnumeric正是你想要的 第一次测试 what = 'letters' i

我正在使用PyCharm,希望知道如何设置字符串/变量来告诉我用户输入的是字母字符还是数字?下面是一个伪代码示例:

weak = all characters or numbers
strong = a mix of characters and numbers

INPUT password

在这种情况下,“isaplpha”或“isnumeric”是否能派上用场?如果是,怎么做?

是的!你是对的,
isalpha
isnumeric
正是你想要的

第一次测试

what = 'letters'
if what.isalpha():
    print('only letters')
elif what.isnumeric():
    print('only numbers')
输出

'only letters'
only numbers
第二次测试

what = "123" 
if what.isalpha():
    print('only letters')
elif what.isnumeric():
    print('only numbers')
输出

'only letters'
only numbers
然后,您可能可以将整个内容放入
while
循环中,以不断提示输入密码,然后在代码中添加
else:break

像这样:

while True:
    user_input = input("Please enter your password: ")

    if user_input.isalpha() or user_input.isnum():
        print("Your password is weak. Consider mixing letters and digits for a stronger password/n")
    else:
        print("Strong password/n")
        break
令人惊叹的例子

Please enter your password: 1234
Your password is weak. Consider mixing letters and digits for a stronger password

Please enter your password: A1B2C3
Strong password

当然,如果你想特别提到密码是全部字母还是数字。您可以添加单独的
if user\u input.isnum():print(“…”
elif user\u input.isalpha():print(“…”
),确保它们能派上用场。你试过用它们吗?问题是什么?这似乎很有帮助,但这里的问题是,我很困惑如何让python告诉用户他们的密码很弱,因为他们输入了所有的数字或字母。这正是解决这个问题的方法。但你只需要接受一个输入,而不是什么。