Python class attribute repeating value
Question:
I would like to know why this check variable repeating the previous value as && instead of entering the new text Bob. How to implement the below code so that Check variable will not repeat the previous value?
I have tried to add check=check.replace(check,"") to make the variable Check blank. However it still taking the old value as &&. How to fix this?
class Game:
alphabet=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def __init__(self):
self.char_list=''
def check_char(self,txt):
for i in txt:
if i not in self.alphabet:
self.char_list+=i
return self.char_list
game=Game()
limit=0
while not limit>4:
text = list(input("Type your message:n").lower())
print(text)
check=game.check_char(text)
print(check)
limit+=1
continue
if limit==5:
break
output:
Type your message:
hello&&
['h', 'e', 'l', 'l', 'o', '&', '&']
&&`enter code here`
Type your message:
bob
['b', 'o', 'b']
&&
Type your message:
Answers:
Each iteration of your while loop is using the same instance of Game. The instance variable char_list is cumulative. You probably don’t want an instance variable. Use a function-local variable instead or even a one-liner that doesn’t require an explicit variable declaration.
from string import ascii_lowercase as ALPHABET
class Game:
def __init__(self):
pass
def check_char(self, txt):
return ''.join(c for c in txt if c not in ALPHABET)
game = Game()
for _ in range(4):
msg = input('Type your message: ').lower()
print(game.check_char(msg))
class Game:
alphabet=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def check_char(self,txt):
char_list=''
for i in txt:
if i not in self.alphabet:
char_list+=i
return char_list
I would like to know why this check variable repeating the previous value as && instead of entering the new text Bob. How to implement the below code so that Check variable will not repeat the previous value?
I have tried to add check=check.replace(check,"") to make the variable Check blank. However it still taking the old value as &&. How to fix this?
class Game:
alphabet=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def __init__(self):
self.char_list=''
def check_char(self,txt):
for i in txt:
if i not in self.alphabet:
self.char_list+=i
return self.char_list
game=Game()
limit=0
while not limit>4:
text = list(input("Type your message:n").lower())
print(text)
check=game.check_char(text)
print(check)
limit+=1
continue
if limit==5:
break
output:
Type your message:
hello&&
['h', 'e', 'l', 'l', 'o', '&', '&']
&&`enter code here`
Type your message:
bob
['b', 'o', 'b']
&&
Type your message:
Each iteration of your while loop is using the same instance of Game. The instance variable char_list is cumulative. You probably don’t want an instance variable. Use a function-local variable instead or even a one-liner that doesn’t require an explicit variable declaration.
from string import ascii_lowercase as ALPHABET
class Game:
def __init__(self):
pass
def check_char(self, txt):
return ''.join(c for c in txt if c not in ALPHABET)
game = Game()
for _ in range(4):
msg = input('Type your message: ').lower()
print(game.check_char(msg))
class Game:
alphabet=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def check_char(self,txt):
char_list=''
for i in txt:
if i not in self.alphabet:
char_list+=i
return char_list