If statement While循环with if语句不起作用

If statement While循环with if语句不起作用,if-statement,while-loop,If Statement,While Loop,只要名称不是“1234”,我就希望while循环运行。如果名称仅为空格,我想打印“名称不能仅为空格”,然后再次要求输入名称。否则我想让它再问一次名字 name=input("") while (name!="1234"): if(name.isspace): print ("name can not be only spaces") name=input("") else: name=input("") 我的问题是,无论我给它什么字符串,它都会打印“nam

只要名称不是“1234”,我就希望while循环运行。如果名称仅为空格,我想打印“名称不能仅为空格”,然后再次要求输入名称。否则我想让它再问一次名字

name=input("")


while (name!="1234"):
  if(name.isspace):
    print ("name can not be only spaces")
    name=input("")
  else:
    name=input("")


我的问题是,无论我给它什么字符串,它都会打印“name不能只是空格”。即使字符串没有空格,它也会打印if语句。发生了什么事?

您可能只是缺少了
isspace
函数的括号。

因此,将
isspace
转换为
isspace()
,您应该会没事。

正如Mat已经提到的,您面临的问题只是因为name.ispace没有返回程序中所需的布尔值

此外,我认为您可以将代码简化一点-

name=input("")


while (name!="1234"):
  if(name.isspace()):
    print ("name can not be only spaces")

  name=input("")