Home > Mobile >  why can't I pass the function as a parameter to the random?
why can't I pass the function as a parameter to the random?

Time:12-22

In my project, I have a function that should return a value from user input and give this value to the function random, but the interpreter says that this parameter should be only a numeric value, and not a method.

def func(self):
    rand = random.randrange(0, self.high)
    print(rand)

def high(self):
    choice = int(input("Type last point: ")
    retrun choice

CodePudding user response:

You're passing in self.high which is a reference to your method. Instead, what you want is pass its return value which you get by calling the method:

def func(self):
    rand = random.randrange(0, self.high())
    print(rand)

Note the parentheses () after the method name, which are crucial here.

CodePudding user response:

first you have typo mistake in return and second - you are calling method so you have to use parentheses () in method name.

import random
class Hello:
    def high(self):
        choice = int(input("Type last point: "))
        return choice

    def func(self):
        rand = random.randrange(0, self.high())
        print(rand)

h = Hello()
h.func()
  • Related