Hướng dẫn given two polynomials a and b, write a program that adds the given two polynomials a and b in python - Cho hai đa thức a và b, viết chương trình cộng hai đa thức a và b đã cho bằng python

Vấn đề của bạn đã xảy ra tại:

if polynomials_sum[i]!=-1:
    if polynomials_sum[i] 0 else print[f' - {abs[coff]}',end='']
        continue
    
    # if the coefficient is not 0 and term is first term, hide the + if coff > 0 and end iteration
    # if p0 != 1, print term 
    if i == len[polynomials_sum]-1 and i != 1: 
        # if coff not 1/-1, print the coff, else just print +/-
        if abs[coff] != 1:
            print[f'{coff}x^{i}',end=''] if coff > 0 else print[f'-{abs[coff]}x^{i}',end='']
        else:
            print[f'x^{i}',end=''] if coff > 0 else print[f'-x^{i}',end='']
        continue

    # if p0 == 1, ignore term 
    if i == len[polynomials_sum]-1 and i == 1: 
        # if coff not 1/-1, print the coff, else just print +/-
        if abs[coff] != 1:
            print[f'{coff}x',end=''] if coff > 0 else print[f'-{abs[coff]}x',end='']
        else:
            print[f'x',end=''] if coff > 0 else print[f'-x',end='']
        continue

    # if the coefficient is not 0 and term is x^1 term, hide the Pi and end iteration
    if i == 1: 
        # if coff not 1/-1, print the coff, else just print +/-
        if abs[coff] != 1:
            print[f' + {coff}x',end=''] if coff > 0 else print[f' - {abs[coff]}x',end='']
        else:
            print[f' + x',end=''] if coff > 0 else print[f' - x',end='']
        continue

    # if coff not 1/-1, print the coff, else just print +/-
    if abs[coff] != 1:
        print[f' + {coff}x^{i}',end=''] if coff > 0 else print[f' - {abs[coff]}x^{i}',end='']
    else:
        print[f' + x^{i}',end=''] if coff > 0 else print[f' - x^{i}',end='']
        
            

Điều này rõ ràng là nhiều

Chỉnh sửa mới cho đầu vào thứ 5:

Bởi vì bạn thiếu đánh giá thuật ngữ đầu tiên trong điều kiện hệ số 0: if i==len[polynomials_sum]-1: #if polynomials_sum[i]

Bài Viết Liên Quan

Chủ Đề