Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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 输入为null,为空_Python - Fatal编程技术网

Python 输入为null,为空

Python 输入为null,为空,python,Python,我有代码检查是否有任何空间或它只是空的。我试着重新制作=、“”、“”、=。但我没有成功。有什么问题吗?当我输入密码时,它总是显示我在打印功能中的错误 while True: password = getpass.getpass("~ Please pick a password, for user - {n}\n".format(n=name)) fontas = " " fontas2 = ' ' if fontas and fontas2 in passwor

我有代码检查是否有任何空间或它只是空的。我试着重新制作=、“”、“”、=。但我没有成功。有什么问题吗?当我输入密码时,它总是显示我在打印功能中的错误

while True:
    password = getpass.getpass("~ Please pick a password, for user - {n}\n".format(n=name))
    fontas = " "
    fontas2 = ' '
    if fontas and fontas2 in password:
        print("~ Password can't contain a spaces!\n")
        continue
    else:
        break
编辑* 我正在添加GIF。当我稍微修改一段代码时,这就向你展示了它是如何工作的

第一次尝试,在我没有键入任何内容之后,我尝试创建一个空格,最后一次我放入了普通关键字-ffawt

*我不能添加GIF文件,所以我正在Gyazo平台上传链接


链接-

您应该使用正则表达式。正则表达式可以检查字符串中的

fontas = password.match(' ')
if fontas:
    print('Match found: ', fontas.group())
else:
    print('No match')
查看文档以了解更多信息。

fontas
fontas2
是一样的,你可以这样做

if ' ' in password:
    print("~ Password can't contain a spaces!\n")
    # ...
一个完整的例子:

import getpass

# ...

name = 'Alex' # considering a user called Alex

while True:
    password = getpass.getpass(
        "~ Please pick a password, for user - {n}\n".format(n=name))
    fontas = " "
    if fontas in password:
        print("~ Password can't contain a spaces!\n")
        continue
    elif not password:
        print("~ Password can't be empty!\n")
        continue
    else:
        break

我相信这就是你想要的:

if password and (" " in password):
    print("BAD")
else
    print("GOOD")

它首先检查是否有密码,然后检查是否有空格

,以检查提供的字符串是否为空,或者是否包含任何空格,然后执行此操作

while True:

    password = getpass.getpass("~ Please pick a password, for user {n}\n".format(n=name))
    fontas = " "
    if fontas in password:
        print("~ Password can't contain a spaces!\n")
        continue
    elif password == '':
        print("~ Password cannot be empty!\n")
        continue
    else:
        break
我希望这有助于:

while True:
    Password = input('Enter a password: ')
    if " " in Password or len(Password) == 0:
        print("~ Password can't contain a spaces!")
        continue

    #if 'if' statement evaluates to True
    print(Password)
    break
编辑:

虽然这将“完成任务”,但在python中还有其他方法可以评估字符串是否为空

空字符串将计算为布尔值(False):

也就是说,上面的代码可以重构为:

while True:
      Password = input('Enter a password: ')
      #Make 'falsy' evaluation of password
      if not Password or " " in Password:
          print("~ Password can't contain a spaces!")
          continue

     #if 'if' statement evaluates to True
      print(Password)
      break

我不明白如果fontas的话,
有什么意义。值不能更改,这完全没有意义。首先,
之间没有区别。第二,如果有,应该是
组合,而不是
。第三,您没有检查密码中是否有两个值,这两个值应该是密码中的
fontas和密码中的fontas2
。如果密码可能是“Rauna51”,则会因为空格而出错,对吗?但如果输入为空怎么办”“空白?我在代码中使用了Fontas=''对不起,空格为''。每个字符串中都有一个空字符串,不管是否为空:“
”中的“
”、“password”
”中的“
”、“password”
中的“
”都是真的。为什么有必要这样做<密码中的code>fontas2很好,而且比正则表达式更有效,问题出在其他所有方面。根本没有帮助。显然没有,否则你就不会遇到这种问题。如果你在处理密码,你可能需要检查的不仅仅是空格,所以正则表达式无论如何都是非常有用的;如果是空字符串,它肯定不会包含空格!别帮我,我不明白这些代码背后的问题是什么。你看,你做了一个代码,可以看到是否有空格,但它不会检查代码是否为空。如果你想检查是否没有提供密码,请检查编辑后的答案。
len(password)==0
对空字符串不是一个很好的检查,而产生的错误消息实际上与输入的问题不匹配。@jonrsharpe打印消息与逻辑不匹配100%,但他需要一个块:检查密码字符串中密码中是否有空格'if',为空
len(Password)==0
。如果你没有向OP解释他的代码有什么问题,你会如何更新这个程序块。您不应该计算长度来检查是否为空。如果这种情况越来越严重,我也不会感到惊讶downvoted@BlueSheepToken,谢谢你提醒我:)!我对Falsy求值而不是字符串长度的谓词字符串空逻辑进行了一些编辑。这样可以更平滑地处理边缘情况
while True:
      Password = input('Enter a password: ')
      #Make 'falsy' evaluation of password
      if not Password or " " in Password:
          print("~ Password can't contain a spaces!")
          continue

     #if 'if' statement evaluates to True
      print(Password)
      break