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
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++'
Comments
Leave a comment