2

I am trying to pass a list of two integers created from one function so that I can manipulate them in succeeding functions.

    def rollDie(number):
        throw = [] 
        for i in range(number):
            roll = random.randint(1,6)
            throw.append(roll)
        return throw

So I have created another function game() that calls the rollDie() results:

    def game(self,number):
        if self.rollDie[0] != 1 or self.rollDie[1] != 1:
            round_score = self.rollDie[0] + self.rollDie[2]
            return round_score

But when I call the function game() it does not pull the two integers from rollDie():

    print(game(2))

it returns error:

    TypeError: game() missing 1 required positional argument: 'number'

I have researched here, here, here among other places inside stackoverflow. I am hoping someone can help. Many thanks for your patience.

2
  • 3
    Why did you give game a self argument? Is it in a class? I think you should read the Python tutorial to get a grasp of the basics of functions in Python. Commented Nov 22, 2015 at 2:38
  • Note that game() is using self.rollDie[2]. You probably meant self.rollDie[1]. Commented Nov 22, 2015 at 2:40

3 Answers 3

2

The way you have defined the function, you have made it a non-static reference, i.e. it has to be called on an object.

You have to call game on whatever object you have defined. For example, instantiate your class as game1 and then call game1.game(2).

Sign up to request clarification or add additional context in comments.

Comments

1

As @AAA pointed out, the way you are defining game() makes it look like a class function (you can read about classes here) but it does not look like you have defined a class anywhere. If you have defined a class elsewhere, then we need to see that code. If you didn't mean to create a class, then you need to take out the self references.

Also, I am not sure how self.rollDie[0] is supposed to work. Are you trying to reference a class list called rollDie? If so, I do not see that defined. If you are trying to call your def rollDie(number): function, then you need to do it like so: self.rollDie(). If you want to access the list indices, it would be best to make that equal to something: self.roll_list = self.rollDie(1) which you can then access by self.roll_list[0]

One thing to keep in mind is that lists are mutable objects, so if you are going to use your lists in multiple functions and you do not intend to create a class, thenit may be less confusing to initiate it on its own, outside of a function, as you can access it from any function.

1 Comment

You are perfectly right about the function call. There are multiple things that are wrong with OP's code as written. I suggest he take another look at the principles of OOP programming.
0

I did a similar program:

Dice statistics 14.11.2015

from random import sample

result = [0, 0, 0, 0, 0, 0] tosses = 10000

for i in range(tosses): dice = sample(range(0, 6), 1) new = str(dice).strip('[]') result[int(new)] += 1

for i in range(0, 6): print(i + 1, ' ====> ', result[i])

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.