Python 为什么我只得到第二个数字?
这两个文本文件的值都在不断增加。当其中任何一个的值大于9时,代码只使用并打印出该数字所在的位置。我如何解决这个问题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
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]