Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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,这两个文本文件的值都在不断增加。当其中任何一个的值大于9时,代码只使用并打印出该数字所在的位置。我如何解决这个问题 h = open('which-quotea.txt', 'r') content = h.readline() # Reading from the file var_a = 0 for line in content: for i in line: if i.isdigit() == True: # Checking for the digit in

这两个文本文件的值都在不断增加。当其中任何一个的值大于9时,代码只使用并打印出该数字所在的位置。我如何解决这个问题

h = open('which-quotea.txt', 'r')
content = h.readline()  # Reading from the file
var_a = 0
for line in content:
    for i in line:
        if i.isdigit() == True:
# Checking for the digit in the string
            var_a = int(i)
print(var_a)
h.close()
h = open('which-quoteb.txt', 'r')
content = h.readline()  # Reading from the file
var_b = 1
for line in content:
    for i in line:
        if i.isdigit() == True:
# Checking for the digit in the string
            var_b = int(i)
print(var_b)
h.close()


对于
numbers>9
var\u a
var\u b
应更新为
var\u a=var\u a*10+var\u a
var\u b=var\u b*10+var\u b.
第一次将
readline
更改为
readlines

i
中的
对于行中的i:
是行中的每个字符

e、 g

使用
split()
或其他方法

list(map(int, line.split()))
# [123, 456]

我看不出您在哪里检查大于9的值。用您自己的话说,当您为行中的I执行
时:
,您希望
I
具有什么值?每次通过循环时,
var_a
(或
var_b
)会发生什么情况?似乎您打算让最终值解释数字中的所有数字。您打算让程序的逻辑如何实现这一点?
list(map(int, line.split()))
# [123, 456]