Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

code mẫu ở bài 23 bị lỗi

16:43 21-08-2021 406 lượt xem 2 bình luậ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

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
AlexanderVeer đã bình luận 16:51 26-08-2021

Đã 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)

K9 SuperAdmin, KquizAdmin, KquizAuthor đã bình luận 17:13 21-08-2021
nhìn code k hợp lý cho lắm

Câu hỏi mới nhất