def __init__(self):
#self.data = []
self.random_word = random.choice(open("EnglishDictionary.txt").readlines()).strip()
self.length_notice = "The word you're guessing is {} letters long.".format(len(random_word))
This just returns the error: Name 'random_word' is undefined
You set self.random_word
, not random_word
, so you need to use self.random_word
:
self.length_notice = "The word you're guessing is {} letters long.".format(len(self.random_word))
# Add self. ^^^^^
Just use it:
def __init__(self):
#self.data = []
with open("EnglishDictionary.txt") as f:
msg = "The word you're guessing is {} letters long."
self.random_word = random.choice(f).strip()
self.length_notice = msg.format(len(self.random_word))
Ask yourself, though, if self.random_word
really needs to be an instance attribute, or if random_word
can simply be a local variable inside the function, like msg
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.