Home > Back-end >  I cen't save vairrabre to text file
I cen't save vairrabre to text file

Time:07-21

I wrote a function that generates a report with calculations. I want to save the report as a text file.

This is my code:

def pom(DF):
    father = len(DF[(DF.kro == 10)])
    mother = len(DF[(DF.krm == 10)])
    allAnimals = len(DF)
    x = print(f'''Numbers of animals {allAnimals}\n
    Numbers of animals where father is wbp - {father}\n
    Numbers of animals where mother is wbp - {mother}''')
    return x

test = pom(DF)

with open('test.txt', 'w') as f:
    f.write(test)

When I use with open I get error:TypeError: write() argument must be str, not None The function should return x print but when I call the test variable nothing is displayed.

CodePudding user response:

The error is in your pom() function, you made x = print()

This means that your x will contain the return value of the print function => None.

Just remove the print() and make x equal to the string between quotes.

If you want to print the result just add a new instruction:

print(x)
  • Related