Hỏi đáp
Chia sẻ kiến thức, cùng nhau phát triển
import math
try:
with open('D:/code python/baitap/bai 23/input.inp','r') as fileIn:
firstLine=fileIn.readline().strip()
if firstLine=='1':
secondLine=fileIn.readline()
a,b=map(float,fileIn.readline().split())
Ở bài tập số 23,nếu mình để trong trường hợp file input là
1
0 0
thì file chạy được
còn nếu thay bằng a,b=map(float,secondLine.split()) thì báo lỗi ngay
nếu bỏ dòng: secondLine=fileIn.readline() thì chương trình cũng báo lỗi luôn
Mong mọi người giải thích rõ chỗ này với
Đã tìm được cách khắc phục lỗi trên:
import math
try:
with open('D:/code python/bai tap python can ban/bai 23/input.inp','r')as fileInp:
firstLine=fileInp.readline().rstrip('\n')
if firstLine=='1':
secondLine=fileInp.readlines()
secondLine=' '.join(secondLine).split()
a,b=map(float,secondLine)