Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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_Python 3.x - Fatal编程技术网

Python 查找字符串值的长度,但不查找数值

Python 查找字符串值的长度,但不查找数值,python,python-3.x,Python,Python 3.x,我试图找到字符串值的长度,但也不需要得到数值的长度 如何解决这个问题?这是我的代码: def CalcStringlen(string): if type(string) == int: return("Sorry integers don't have length") else: return len(string) string = input("Enter the string") print(CalcStringlen(string))

我试图找到字符串值的长度,但也不需要得到数值的长度

如何解决这个问题?这是我的代码:

def CalcStringlen(string):
    if type(string) == int:
        return("Sorry integers don't have length")
    else:
        return len(string)

string = input("Enter the string")
print(CalcStringlen(string))
使用

输出:

Enter the string: 1234                                                                                                                         
Sorry integers don't have length  
使用

输出:

Enter the string: 1234                                                                                                                         
Sorry integers don't have length  

您的
输入
调用正在返回字符串。即使其中只有数字,它仍然是一个字符串,其类型永远不会是
int
。更改此测试:

if type(string) == int:


您的
输入
调用正在返回字符串。即使其中只有数字,它仍然是一个字符串,其类型永远不会是
int
。更改此测试:

if type(string) == int: