हिंदी

In the code (given above), the word END used to indicate end of word list is also stored in the file. Modify the code so that end is not stored in the file.

Advertisements
Advertisements

प्रश्न

file = open('textfile.txt','w')
word = ''
while word.upper() != 'END':
     word = raw_input('Enter a word use END to quit')
     file.write(word + '\n')
file.close()

In the code (given above), the word END used to indicate end of word list is also stored in the file. Modify the code so that end is not stored in the file.

कोड लेखन
Advertisements

उत्तर

with open("textfile.txt", "w") as file:
    while True:
        word = input("Enter a word (END to quit): ")
        if word.upper() == "END":
            break
        file.write(word + "\n")

The termination test is performed before write(), so END is not stored.

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 7: Data File Handling - EXERCISE [पृष्ठ १४५]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 7 Data File Handling
EXERCISE | Q 12. | पृष्ठ १४५
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×