Answer to Question #306049 in Python for Michael

Question #306049

Write a python function Write_String(fname, *list)  which take a list containing string elements and write them in a file myfile.txt until the string “finish” (not case sensitive) occur. The function then returns the contents written in file as a string. Before writing list elements in file, you also need to make a separate python function to check whether input list contains "Finish" or not. If not, then it should return error as shown in example.


Write the program in function definition?


myfile.txt:

Python

Java

C

C++

Ruby

.Net


 

Example-1

Example-2

Example-3

Input:

myfile.txt

["Python", "Java", "C", "C++", "Finish"]

 

Output:

PythonJavaCC++

Input:

myfile.txt

["Python", "Java", "C", "C++", "Finish",".Net", "Rubby"]

Output:

PythonJavaCC++Input:

myfile.txt

["Python", "Java", "C", "C++"]

Output:

Error: List does not contain Finish

 


1
Expert's answer
2022-03-07T12:55:55-0500
def check_list(list1):
    if 'Finish' not in list1:
        return False
    else:
        return True



def Write_String(fname, list1):
    string1 = ''
    file = open(fname, 'w+')
    if check_list(list1) == True:
        for i, j in enumerate(list1):
            file.write(j)
            string1 = string1 + j
            if list1[i+1] == 'Finish':
                break
        return string1
    else:
        return 'list does not contain finish'



Write_String('my_file.txt', ["Python", "Java", "C", "C++", "Finish",".Net", "Rubby"])
        
'PythonJavaCC++'

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS