runtime
183 / 500
question 183 Files & Directories · medium

Add a subscriber only once

A newsletter keeps its subscribers in a text file, one email address per line. Sign-ups arrive with stray spaces and capital letters, and people sign up twice. Write a function that adds an address only if it isn't already in the file.

Functionadd_subscriber(path, email)
Compareaddresses are compared after strip() and lower(), on both sides
Effectwhen new, the cleaned address (stripped, lower case) is added on its own line at the end; a missing file is created
ReturnsTrue if the address was added, False if it was already there
Rulesevery existing line ends with a newline; blank lines in the file are ignored
Examples
calladd_subscriber('subscribers.txt', 'SAMUEL_MENSAH@yahoo.com')returnsFalse
calladd_subscriber('fresh.txt', 'mei.adams48@gmail.com')returnsTrue
calladd_subscriber('subscribers2.txt', ' Aisha.Silva@gmail.com ')returnsTrue

Loading your progress…

Add a subscriber only once — Python practice · Runtime