Write a code snippet that will create an object called fileout for writing; associate it with the filename STRS. The code should keep on writing strings to it as long as the user wants.

Here’s a code snippet that creates an object called fileout for writing and associates it with the filename “STRS”. The code continues writing strings to the file as long as the user wants:

fileout = open("STRS", "w")
keep_writing = True

while keep_writing:
    string = input("Enter a string (or 'q' to quit): ")
    if string.lower() == "q":
        keep_writing = False
    else:
        fileout.write(string + "\n")

fileout.close()

This code opens the file “STRS” in write mode and assigns it to the fileout object. It then prompts the user to enter strings, which are written to the file. The loop continues until the user enters ‘q’ to quit. Finally, the file is closed using close().


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *