beepbeep-boop
beepbeep-boop

Reputation: 129

Pass a value from a function to another function

I have a piece of code, disclaimer: I have not written a lot this code, others have helped. and i want to pass a parameter within it.

This is supposed to turn a binary number into a decimal, further converting the decimal to a binary.

If you look near the bottom, there's an input, taking 1111 as an example binary number, it turns into 15 as a decimal, which i want 15 to turn into a hexadecimal, not 1111.

How do i make it so that the second function, two() uses 15? I have a class and a constructor, i want to know how i can pass the end result of the function one() to the function two().

import os,time

class Helper:

    def __init__(self, num):
        self.num = num

    def one(self):
        b1 = self.num
        b2 = 0
        d1 = 0
        p = 0
        while(b1 != 0): 
            b2 = b1 % 10
            d1 = d1 + b2 * pow(2, p)
            b1 = b1//10
            p = p + 1
        print (d1)


    def two(self):
        n = self.num
        hex_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']
        reversed_number = ""
        while n > 0:
            remainder = n % 16
            n -= remainder
            n //= 16
            reversed_number += str(hex_values[remainder])

        print(reversed_number[::-1])


os.system('clear')
print
print ("Input any number.")
n = Helper(int(input(">> ")))

time.sleep(1)
n.one()

time.sleep(1)
print
n.two()
print
time.sleep(1)

If you look at the function one() there is a print, i want to pass that value, not the variable b1 or the value of the input the user gave.

Upvotes: 0

Views: 94

Answers (3)

Nicholas Obert
Nicholas Obert

Reputation: 1628

Here is s way to solve it

import os,time

class Helper:

    def __init__(self, num):
        self.num = num

    def one(self):
        b1 = self.num
        b2 = 0
        d1 = 0
        p = 0
        while(b1 != 0):
            b2 = b1 % 10
            d1 = d1 + b2 * pow(2, p)
            b1 = b1//10
            p = p + 1
        print (d1)
        return d1 # set the output of the function


    def two(self, n):
        hex_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']
        reversed_number = ""
        while n > 0:
            remainder = n % 16
            n -= remainder
            n //= 16
            reversed_number += str(hex_values[remainder])

        print(reversed_number[::-1])


os.system('clear')
print
print ("Input any number.")
n = Helper(int(input(">> ")))

time.sleep(1)
decimal = n.one() # you define a variable as the output of n.one()

time.sleep(1)
print
n.two(decimal) # you pass the output of n.one() to n.two()
print
time.sleep(1)

You can set an output for your function n.one() using the return keyword the syntax is: return {value}, so if the value you want to return is d1 simply type

return d1

Use a variable to capture the output of n.one

decimal = n.one()

at this point to display the output of n.one() you could type

print(decimal)

instead of printing it in the function n.one() now make the function n.who() take the argument you passed

def two(self, n):

and delete n = self.num

finally you pass the output of n.one() to n.two()

n.two(decimal)

Hope this solved your issue

Upvotes: 1

LukasNeugebauer
LukasNeugebauer

Reputation: 1337

You need to return the value instead of printing it.

def one( self ):
    [...]
    return d1

If you only need the output of one() as input to two() you can also skip the assignment to a variable. one() evaluates to a numeric value and you can directly pass it.

h = Helper( int(input( ">> " )) );
h.two( h.one() );

Or you make it part of the object, then you can just use it.

def one(self):
    [...]
    self.d1 = d1;

def two(self):
    value = self.d1;
    [...]

Upvotes: 0

olisch
olisch

Reputation: 1000

your one method needs to return a value instead of printing it, so that you can save the value to a variable. Your two method then needs a parameter in order to pass the previous value.

def one(self):
    ...
    return d1

def two(self, value):
    ...

v = n.one()
n.two(v)

As an altenative, you can save the result of one method to an additional instance variable.

Upvotes: 1

Related Questions