Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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 - Fatal编程技术网

如何在Python中以字符串格式返回整数?

如何在Python中以字符串格式返回整数?,python,Python,我想做一个简单的方法如下: def name(self): n = str(self.count) self.count += 1 return n 不幸的是,“str”将两位数修改为单独的字符串,每个数字对应一个字符串。如何将两位数保留为单个字符串 编辑: 您可以看到下面的输出。我已经包括了很多打印的声明 f to be updated: 8 a after update: {'2', '6', '8', '4'} a: {'2', '6', '8', '4'}

我想做一个简单的方法如下:

def name(self):
    n = str(self.count)
    self.count += 1
    return n
不幸的是,“str”将两位数修改为单独的字符串,每个数字对应一个字符串。如何将两位数保留为单个字符串

编辑:

您可以看到下面的输出。我已经包括了很多打印的声明

f to be updated:  8
a after update:  {'2', '6', '8', '4'}
a:  {'2', '6', '8', '4'}
dom:  {'2': '0', '6': '0', '4': '1', '8': '5'}
cod:  {'2': '1', '6': '5', '4': '3', '8': '7'}
Key:  2
Key:  6
Key:  4
Key:  8
Key:  2
Key:  6
Key:  4
Key:  8
Dom:  {'5', '1', '0'}
Cod:  {'5', '1', '3', '7'}
s:  t
y:  9
o:  {'1', '5', '3', '7', '9'}
e:  10
f to be updated:  10
a after update:  {'1', '6', '4', '0', '2', '8'}
a:  {'1', '6', '4', '0', '2', '8'}
dom:  {'2': '0', '6': '0', '4': '1', '10': '0', '8': '5'}
cod:  {'2': '1', '6': '5', '4': '3', '10': '9', '8': '7'}
s:  t
您将看到'f to update'是要添加到集合'a'的字符串的名称。在每一次迭代中,它都会按其应该的方式发生;然而,当到达两位数的10时,程序将输入两个一位数的数字。

尝试以下操作:

global count 
count = 0
def n():
    global count
    return str(count)
    count += 1

test_val = n()
print(type(test_val))

“str”将两位数修改为单独的字符串,每个数字都是开的”,这是什么意思?您能举一个运行代码时出现的输出示例吗?我不确定您在这里尝试做什么。str()不分隔两位数的两个数字。另外,在函数中,return语句后面有一个语句,这是为了什么?