我创建了一个返回以下错误的函数:
original_alphabet.remove(value)
AttributeError: 'str' object has no attribute 'remove'
我不知道如何修复错误,感谢任何帮助。这是我的代码:
def keyword_cipher_alphabet(keyword):
original_alphabet = "abcdefghijklmnopqrstuvwxyz"
for value in original_alphabet:
if value in keyword:
original_alphabet.remove(value)
keyword_alphabet = ""
for value in original_alphabet:
keyword_alphabet += value
user_keyword = ""
for value in keyword:
user_keyword += value
result = user_keyword + keyword_alphabet
return result.upper()
答案 0 :(得分:1)
导致错误是因为字符串没有方法remove
。您可以尝试使用replace
:
$> my_str = 'abc'
$> my_str = my_str.replace('b', '')
$> my_str
'ac'
答案 1 :(得分:0)
尝试将字符串转换为列表,然后将其删除。
class UserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
user = self.model(email=self.normalize_email(email), **extra_fields)
user.set_password(password)
user.save(using = self._db)
return user
def create_superuser(self, email, password=None):
user = self.create_user(email, password)
user.is_admin=True
user.is_staff=True
user.is_superuser=True
user.save(using = self._db)
return user
class Account(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(verbose_name='email', unique=True, max_length=60)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
# REQUIRED_FIELDS = ['username',]
objects = UserManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj = None):
return self.is_admin
def has_module_perms(self, app_lebel):
return True