以你为例,我的方法是:
#Returns a tuple where the first item is "False" if the email already exists in database,
#and "True" if it doesn't and it was appended to the database. The second item is the email.
def get_or_create_customer (email , database): #email as String, database as list
if email in database:
return (False, email)
else:
database.append(email)
return (True, email)
测试代码:
database = ["hello@gmail.com"]
print (get_or_create_customer("hello@gmail.com", database), database)
>> (False, 'hello@gmail.com') ['hello@gmail.com']
print (get_or_create_customer("example@gmail.com", database), database)
>> (True, 'example@gmail.com') ['hello@gmail.com', 'example@gmail.com']