Reuse of Python functions

Asked

Viewed 249 times

1

I am a beginner in programming and I have the following situation:

  • I have a class called TEST. Within this class has the function RANDOM.

  • I have a second class called CHECK. Inside this class has the CHECK function.

  • I want the output of the TEST.RANDOM function to be used in the CHECK function.CHECK.

  • In the code below every time I run the function CHECK.VERIFIES it calls again the function TEST.RANDOM, which ends up bringing me a new value.

  • How do I REUSE the output of the TEST.RANDOM function?

=========

import random

class teste():
    def __init__(self, x = 0):
        self.x = x

def aleatorio(self):
    a = random.randint(0,100)
    return a

class verificar():
    def __init__(self, y = 0):
        self.y = y

    def verifica(self):
        b = teste().aleatorio()
        return b

print (teste().aleatorio())
print (verificar().verifica())

==========

In advance, I thank everyone who is willing to help.

  • You want only one random value to be generated and used in both calls?

  • I want the random value, generated in the TEST.RANDOM function, also to be used in the CHECK function.CHECK.

1 answer

1


Do the following:

import random

class teste():
    def __init__(self, x = 0):
        self.x = x
        self.aleatorio()

    def aleatorio(self):
        self.a = random.randint(0,100)
        return self.a

class verificar():
    def __init__(self, y = 0):
        self.y = y

    def verifica(self):
        b = teste()
        return b.a

If you need to call aleatorio() again, you can do the following:

class verificar():
    def __init__(self, y = 0):
        self.y = y
        self.teste = teste()

    def verifica(self, chamar_de_novo = False):
        if (chamar_de_novo):
            self.teste.aleatorio()
        return self.teste.a

Note that here:

print (verificar().verifica())

You’re using an object verifica different from here:

print (teste().aleatorio())

That’s why the value doesn’t hold. You can do, for example, verificar receive an object teste to at least keep the random number generated:

v = verificar()
teste = teste()
teste.aleatorio()
v.teste = teste
  • when I run, it gives the error " Typeerror: randomly() takes Exactly 1 argument (0 Given)" ?

  • My indentation error. See my edit, please.

  • Fixed identation, now gives error: "Nameerror: global name 'aleatorio' is not defined"

  • Sorry, I did not test this code. I edited again.

  • No problem. I’m here to learn. Look, when I "printed" the functions. See the output: 28, 56. I wanted the function of the class CHECK.CHECK, returns 28 also Sorry to bother, I’m novice.

  • @Andersoncarloswoss is right. I’m going to edit it one more time.

  • @Danilo The way you are, there’s no way you can keep the numbers. I put a few more suggestions.

  • I won’t be able to help you now. I’m with some candidates this morning to evaluate. See this chat here.

  • I did it. Gypsy! Thank you for your patience!! Thank you very much!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.