1

I'm having a bit of a problem using classes while using a random in a while loop. The goal is the first time to generate a random string and to use that throughout the lesson. Here are the four lines with the problem.

 def run_review(self, key_history):
        level = reviewer.ReviewLesson(self.history_list)
        if self.counter:
            level.shuffled()
            self.counter = False
            return shuffle
        level.review()
        level.review_lesson()

from another module

def shuffled(self):
        self.shuffled_list = sorted(self.history_list, key=lambda k: random.random())
        return self.shuffled_list

    def review(self): 
        for item in self.shuffled_list:
            self.word = item+self.word
        self.word = self.word*7
        return self.word


    def review_lesson(self):
        word = ''
        self.word = word+self.word
        return self.word

The issue is after the first block the review_lesson is an empty space since level.review() is not called, however if level.review is called it then becomes another random sequence.

Edit: Made the code a bit more clearer and removed duplicate functions.

7
  • move the random call outside the method also why are you adding an empty string to self.word and assigning self.word = ... twice? Commented Feb 21, 2015 at 20:12
  • @poor coding for the second. The def run_review is a while loop that runs the loop for a pygame but as your suggestion I have edited as show: Commented Feb 21, 2015 at 20:57
  • self.word + "" is the same as just self.word, If you only want a single value just declare the shuffled_list as a class attribute like in my answeror an instance attribute depending on what you want. Then use it in the method, that way it will only be created once Commented Feb 21, 2015 at 20:58
  • you are indeed correct, my only problem is that self.history_list is it is define in the init so now it won't let me define that. Commented Feb 21, 2015 at 21:19
  • I am not sure I follow, why can't you define it as an attribute? Commented Feb 21, 2015 at 21:19

1 Answer 1

1

You seem to be assigning self.word to multiple things in your methods so not fully sure what that is doing but to be able to use review and get the same random shuffled_list each time declare shuffled_list as a class attribute:

 # either declare before init, or use self.shuffled_list = .. in your init method
 shuffled_list = sorted(self.history_list, key=lambda k: random.random())

def review(self): 
        for item in class_name.shuffled_list:
            self.word = item+self.word
        self.word *= 7
        return self.word

For example:

 class Foo():
    r = randint(1, 7) # either here as class attribute so it will be the same for all instances
    def func(self):
        return  Foo.r

## -- End pasted text --

In [13]: for _ in range(5):
            f = Foo()
            print(f.func())
            f1 = Foo()
            print(f1.func())
   ....:     
2
2
2
2
2
2
2
2
2
2

A class attribute will keep it the same for all instances:

class Foo():
    r = randint(1, 7) 
    def func(self):
        return  Foo.r

If you wanted a different value for each instance make it an instance attribute:

    class Foo():
        def __init__(self)
            self.r = randint(1, 7) 
        def func(self):
            return  self.r
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.