cdcdcd
cdcdcd

Reputation: 569

Spyder throwing an error when calling methods from another package/module with the correct number of arguments

Currently every time I try to run this:

       start_1 = Random.randint(0, 25)

I get:

TypeError: randint() missing 1 required positional argument: 'b'

But the definition, and the code completion suggests only two arguments. And yes I am importing Random.

As requested the entire code:

# -*- coding: utf-8 -*-
"""
Created on Fri May 19 22:01:54 2017

@author: 
"""
from random import Random

Random.randint(0, 25)

Upvotes: 2

Views: 1282

Answers (3)

MarianD
MarianD

Reputation: 14191

Random with capital R is the class - so you need instantiate it:

from random import Random           # Random with capital R is the class
random = Random()                   # Instantiate this class
random.randint(0, 25)               # Calling a method of the *object*

Of course, the last 2 lines you may substitute with

Random().randint(0, 25)             # creating object of class Random just for this purpose

Why ...missing positional argument: 'b' in your (incorrect) use?

The function randint() is defined in the class Random by the standard way as

def randint(self, a, b):

so it needs 3 arguments - and you provided only 2.

(The argument self is a special (hidden) argument - in the case of the correct use it will be automatically substituted by the calling object itself).

More comfortable possibilities are

import random
random.randint(0, 25)

or

from random import randint
randint(0, 25)

as randint is in the random module (cleverly, for our convenience) defined as

randint = Random().randint          # Create temporary object and get its method

Upvotes: 3

pkisztelinski
pkisztelinski

Reputation: 542

you should import only random

import random

print(random.randint(1,20))

Upvotes: 2

Is that the entire program and if not can you give us the entire program? Also, try doing: start_1 = random.randint(randint(0, 25))

Upvotes: 0

Related Questions