简体   繁体   中英

AttributeError: 'int' object has no attribute 'splitlines' When Saving Output Input To File

I am getting a "AttributeError: 'int' object has no attribute 'splitlines'" when I go to save the users input to a file.

def testfunction():
    global names, type, length

    while True:
        name = input("Name: ")
        type = input("Type: ")

        if type == "Length":
            length = input("Length: ")

            output_file = open("Test.txt", "w")
            output_file.write(f"""
            {length}
            """).splitlines()
            output_file.close()


        return name, type

testfunction()

output_file.write returns the number of characters written in the file, so you're calling .splitlines() on a int. You should probably remove the .splitlines() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM