shell ValueError中的Python错误:以10为基数的int()的文本无效:
我正在写一个程序。如果短匹配与长匹配混合。该程序将查看哪些匹配项将放回短框中。 这是我的代码:shell ValueError中的Python错误:以10为基数的int()的文本无效:,python,regex,shell,int,Python,Regex,Shell,Int,我正在写一个程序。如果短匹配与长匹配混合。该程序将查看哪些匹配项将放回短框中。 这是我的代码: import math def q2(): fin = open('input.txt','rt') fout = open('output.txt', 'wt') a= int(fin.readline().strip()) b = [int(b) for b in str(a)] count = -1 nMatches = b[0] wid
import math
def q2():
fin = open('input.txt','rt')
fout = open('output.txt', 'wt')
a= int(fin.readline().strip())
b = [int(b) for b in str(a)]
count = -1
nMatches = b[0]
width = b[1]
heigth = b[2]
length = width**2 + heigth**2
length = length**(.5)
while count <= nMatches:
match = int(fin.readline().strip())
count = count+ 1
if match <= length:
print('YES')
else:
print('NO')
导入数学
def q2():
fin=open('input.txt','rt')
fout=open('output.txt','wt')
a=int(fin.readline().strip())
b=[int(b)表示str(a)中的b]
计数=-1
n匹配=b[0]
宽度=b[1]
高度=b[2]
长度=宽度**2+高度**2
长度=长度**(.5)
虽然count看起来像是要将空字符串转换为int。但这是不可能的。
看看你的异常:。。。基数10:''问题在于循环计数。在计数而不是,为什么不只对范围内的i(n匹配)使用
?因为循环计数已关闭,所以读取的内容超过了文件的结尾,当您尝试转换空字符串时会出现错误。我在这里做了很多假设,因为我不知道fin
文件中有什么内容。这就是我认为您正在尝试的:
nMatches=534
是框中的匹配数。
width=3
和height=4
以及导致框的长度为5的
我假设fin
文件中前3个之后的所有数字都是每个匹配的长度
from math import sqrt
def q2():
fin = open('text.txt','r')
fout = open('output.txt', 'w')
a = fin.readline().strip().split()
b = [int(x) for x in a]
nMatches = b[0]
width = b[1]
height = b[2]
length = sqrt(width**2 + height**2)
for match in b[3:]:
if match <= length:
print ('YES')
else:
print ('NO')
q2()
从数学导入sqrt
def q2():
fin=open('text.txt','r')
fout=open('output.txt','w')
a=fin.readline().strip().split()
b=[int(x)表示a中的x]
n匹配=b[0]
宽度=b[1]
高度=b[2]
长度=平方米(宽度**2+高度**2)
对于b[3:]中的匹配:
如果匹配fin
的内容是什么?您可以打印match
并查看您得到的值吗?也许我错了,但异常是否不仅仅表明他试图将空字符串“”转换为int?10秒,查找4个站点以解决此问题。
from math import sqrt
def q2():
fin = open('text.txt','r')
fout = open('output.txt', 'w')
a = fin.readline().strip().split()
b = [int(x) for x in a]
nMatches = b[0]
width = b[1]
height = b[2]
length = sqrt(width**2 + height**2)
for match in b[3:]:
if match <= length:
print ('YES')
else:
print ('NO')
q2()