Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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中的Isspace函数_Python_Function_Conditional Statements_Isspace - Fatal编程技术网

python中的Isspace函数

python中的Isspace函数,python,function,conditional-statements,isspace,Python,Function,Conditional Statements,Isspace,我在执行这段代码时遇到问题,我希望用户输入一个值,程序检查该值是否为字符串,然后返回长度。 如果该值包含空格,程序将删除空格并打印长度。 但如果它包含任何整数值,程序将返回“不允许使用整数” 代码如下: def length(Name): long = len(Name) return long new_length = input("Please Enter Your name You can use Spaces: ") value1 = new_length if val

我在执行这段代码时遇到问题,我希望用户输入一个值,程序检查该值是否为字符串,然后返回长度。 如果该值包含空格,程序将删除空格并打印长度。 但如果它包含任何整数值,程序将返回“不允许使用整数”

代码如下:

def length(Name):
    long = len(Name)
    return long

new_length = input("Please Enter Your name You can use Spaces: ")
value1 = new_length
if value1.isspace() == True:
  print("This is Before Removing Spaces: " + value1)
  value2 = value1.replace(" ", "")
  print("This is After Removing Spaces: " + value2)
elif value1.isalpha() == True: 
  print("Here is The Length: ", length(value1))
elif value1.isdigit() == True:
    print("Integers are not allowed! ")
else:
    print("There's someting wrong with  "+ value1)
如果你能帮我,我很感激。
谢谢

您可以使用此功能检查输入字符串是否包含数字:

def hasNumbers(inputString):
        return any(char.isdigit() for char in inputString)
如果有数字,则返回true;如果没有,则返回false

至于空格,您可以使用ommit isspace()。即使没有空格,单独使用replace()也可以完成这项工作

stri='jshsb sjhsvs jwjjs'
stri=stri.replace(' ','')

可以使用Python中的re模块检查字符串中的空格。如果有空格,则返回True,否则返回False

import re
def length(Name):
    long = len(Name)
    return long

new_length = input("Please Enter Your name You can use Spaces: ")
value1 = new_length
if re.search('\s', value1):
  print("This is Before Removing Spaces: " + value1)
  value2 = value1.replace(" ", "")
  print("This is After Removing Spaces: " + value2)
  print("Here is The Length: ", length(value2))
elif value1.isalpha() == True: 
  print("Here is The Length: ", length(value1))
elif value1.isdigit() == True:
    print("Integers are not allowed! ")
else:
    print("There's someting wrong with  "+ value1)

我不认为
str.isspace
str.isalpha
str.isdigit
方法能达到您期望的效果。首先,它们都测试您输入的字符串中的所有字符是否都是其名称中描述的类型。如果任何字符匹配,您的代码似乎希望它们返回
True
。也就是说,如果存在任何空格,则要删除它们并显示前后两个长度

在Python中,没有一个字符串方法可以为您执行该测试。您可以使用正则表达式(更强大,但更复杂),也可以编写一些稍微复杂的代码来进行测试。我建议使用
any
函数,并向其传递一个生成器表达式,该表达式对字符串中的每个字符调用所需的方法

if any(c.isspace() for c in user_str):
    ...
这可能不是所有测试所需要的。您的代码所需的逻辑并不完全明显,因为您的输出并没有专门针对一些特殊情况。包含字母和数字的字符串有效吗?一个在数字之间有空格但根本没有字母的怎么样?您可能需要对
if
/
elif
/
else
语句的条件进行重新排序,以便它们符合您的要求


我还要注意,您用于用户输入的变量名,
new\u length
,非常容易引起误解。这不是长度,而是你想测量的字符串长度!对于具有误导性或不清楚变量名称的变量,更容易产生逻辑错误,因此花时间回去重新考虑您先前选择的名称有时是个好主意,因为它可以大大提高代码的清晰度!描述性变量名很好,但它是清晰和简洁之间的一种折衷,因为长名称的键入很繁琐(而且容易出错)。它们还可能导致行长度问题,这会降低在编辑器屏幕上同时查看所有代码的便利性。

我建议在这些情况下阅读文档。对于
isspace
,请注意

如果字符串中只有空白字符且至少有一个字符,则返回True,否则返回False

也就是说,如果有任何东西不是一个空格,它将是假的。这很烦人,但为什么要先检查呢?只要做替换!如果没有空格,它就什么也做不了。如果您需要打印这些语句,您可以这样做

如果值1中有“”:
...

(当然,这不考虑所有可能的空白类型,如果需要的话,检查其他for for for循环的答案)

接下来,我认为您需要删除
elif
s,只需使用
if
语句,因为请注意,如果您输入一个带有空格的名称,它将打印删除空格的名称。。。之后就什么都没有了。即使里面有整数也不行。这是因为
elif
语句不会在它们之前执行一次


有很多其他的事情需要你考虑,但是这两个我认为你应该首先考虑。希望有用

关于空格,很容易检查它是否包含整数。如果你能帮我,我很感激。帮你什么,什么问题?