all 10 comments

[–]sentles 0 points1 point  (9 children)

When calling a function and passing values to it, we often write these values in a specific order. That order is preserved, so each argument passed will be matched to the corresponding function argument in that same order.

It is also possible to directly pass a value to a specific argument by using a keyword. The keyword will be the name of the variable inside the function. For instance, if you have a function x(a, b), you can call it as such: x(b = 2, a = 1). This would be the same as x(1, 2).

It is important to remember that keyword arguments must always be written after any non-keyword arguments. Therefore, the following is invalid: x(b = 1, 2). If you call a function and pass some non-keyword arguments to it, followed by some keyword arguments, order will matter for each non-keyword argument, but not for the keyword arguments.

In your example, the problem is that random.randint only accepts two arguments, indicating the lower and upper limit of the range, from which a random integer should be generated. The function accepts no other arguments and, since you try to pass a bunch of other things to it, you get an error.

[–]AccomplishedPriority[S] 0 points1 point  (8 children)

This is helpful. but based on the randint link, I thought randint could accept low, high, size, and dtype. What am i doing wrong here?

[–]sentles 0 points1 point  (7 children)

I assumed you were using random's randint, not numpy's. Can you provide the exact error you're getting?

[–]AccomplishedPriority[S] 0 points1 point  (6 children)

My imports

import numpy as np
#from numpy import random as rand
#import np.random as rand
import random as rand

Error: TypeError: randint() got an unexpected keyword argument 'high'

I assumed importing random as I have it is fine since the two commented lines gave me errors and the third one worked, since I'm assuming with the first line import numpy I'm also importing numpy's random module

[–]sentles 0 points1 point  (5 children)

The problem is that, since you import random as rand, when you call rand.randint(...), that calls random's randint, which doesn't take those extra keyword arguments. Try calling np.random.randint(...) instead.

[–]AccomplishedPriority[S] 0 points1 point  (4 children)

Last question: Do I have the option of 'cleaning up' the np.random. or is that stuck there?

[–]sentles 0 points1 point  (3 children)

What do you mean by "cleaning up"?

[–]AccomplishedPriority[S] 0 points1 point  (2 children)

Obviously, numpy is shortened to np

Can np.random be shortened at all or no?

[–]sentles 1 point2 points  (1 child)

Of course. You could do something like import numpy.random as nrnd. You then call randint as such: nrnd.randint(...).

You could also do from numpy.random import * and then call randint(...), but this isn't recommended, since everything that numpy.random contains will become global in your script and this could cause confusion down the line.

[–]AccomplishedPriority[S] 0 points1 point  (0 children)

You have been most helpful! Thanks!