Answer to Question #305352 in Python for Michael

Question #305352

Write a python function Write_String(fanme, *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.


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-04T02:15:05-0500
with open("my file", "r") as f:
    text = f.read()
a = text.split("\n")
if "Finish" in a:
    a = a.replace("Finish", "", 1)
    print(a)
else:
    print("Error: List does not contain Finish")


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