【发布时间】:2015-01-12 20:51:42
【问题描述】:
我正在编写一个程序。如果短匹配与长匹配混合。该程序是查看哪些匹配项将适合短框。 这是我的代码:
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')
这是输出:
YES
YES
YES
NO
NO
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
q2()
File "D:/code/Q2/q2.py", line 15, in q2
match = int(fin.readline().strip())
ValueError: invalid literal for int() with base 10: ''
感谢您的帮助 fin的内容是: 534 3 4 5 6 7
【问题讨论】:
-
fin的内容是什么? -
你能打印
match看看你得到了什么价值吗? -
也许我错了,但异常不仅表明他正在尝试将空字符串 '' 转换为 int 吗? 10 秒找到 4 个站点来解决这个问题。