Computer Science

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

Python File Handling

2 Likes

Answer

with open('STRS.txt', 'w') as filout:
    ans = 'y'
    while ans == 'y':
        string = input("Enter a string: ")
        filout.write(string + "\n")  
        ans = input("Want to enter more strings?(y/n)...")    
Output
Enter a string: Hello
Want to enter more strings?(y/n)...y
Enter a string: world!
Want to enter more strings?(y/n)...n

The file "STRS.txt" includes:

Hello
world!

Answered By

2 Likes


Related Questions