Question: How to fix this type error without type casting? Traceback (most recent call last): File D:/.Programming/Python/L6_Jiang_Tommy_tkj5216.py,line 54, in main() File D:/.Programming/Python/L6_Jiang_Tommy_tkj5216.py,line 51, in main write_to_file(created_file.txt,test_lst)

How to fix this type error without type casting?def write_to_file(filename,Ist): infile = open(filename,"w") if Ist == []: print("list is empty")

Traceback (most recent call last): File "D:/.Programming/Python/L6_Jiang_Tommy_tkj5216.py",line 54, in main() File "D:/.Programming/Python/L6_Jiang_Tommy_tkj5216.py",line 51, in main write_to_file("created_file.txt",test_lst) File "D:/.Programming/Python/L6_Jiang_Tommy_tkj5216.py",line 32, in write_to_file infile.write(lst+"")TypeError: can only concatenate list (not "str") to list

def write_to_file(filename,Ist): infile = open(filename,"w") if Ist == []: print("list is empty") else: for line in Ist: infile.write(Ist+" ") infile.close def append_to_file(filename,Ist): infile = open(filename,"a") if Ist ==[]: print("list is empty") else: for line in Ist: infile.write(Ist+" ") infile.close def main (): test_lst [1,2,3,4] addition_Ist=[5,6,7,8] empty_file("testfile.txt") create_file("created_file.txt","w") #result = read_file("read_from_this_file.txt") #result_line = read_file_lines("read_file_lines") write_to_file("created_file.txt",test_lst) append_to_file("created_file.txt",addition_Ist) main()

Step by Step Solution

3.41 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

There are a few errors in the code In the writetofile function Ist should b... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!