Answer to Question #305884 in Python for Michael

Question #305884

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-04T06:31:49-0500
'''
    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
'''


def Write_String(fname, List):
    s = ""
    if((List[len(List)-1]).lower()!="finish"):
        s = "Error: List does not contain Finish"
    else:
        
        for r in range(0,len(List)-1):
            s = s + str(List[r])
        outfile  = open(fname,"w")
        outfile.write(s)
        outfile.close()
    return(s)


InputStr = ["Python", "Java", "C", "C++", "Finish"]
s = Write_String("myfile.txt",InputStr)
print("Output: ",s)

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