0

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.

11
  • I know this might seem like a repeat, there is a similar question to this, however it is in completely different context, and i'm not able to understand that very well ;) Commented Jan 2, 2020 at 13:23
  • 3
    The fundamental problem here is that your code simply prints stuff to the screen, and then basically forgets it. The function should return the value rather than just print it (and actually it would probably be a better design to only return and have the caller print or do whatever it wants with the returned value). Commented Jan 2, 2020 at 13:28
  • A number has one value, regardless of the base it's represented in. If you want different representation of the same number try: print("{0:d} {1:X} {2:b}".format(15, 15, 15)). Conversely, check docs.python.org/3/library/functions.html?highlight=int#int. So, the whole (end goal of the) exercise looks a bit like reinventing the wheel. Commented Jan 2, 2020 at 13:29
  • so i should do return d1? Commented Jan 2, 2020 at 13:29
  • Yeah, do that and take it from there. The rest of the code needs to be adapted correspondingly of course. Commented Jan 2, 2020 at 13:30

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Uh, it still used the input as the num for the function two()
It works for me. Try to copy and paste to be sure you didn't miss anything
IT WORKED :D Thanks~! i realised i had an error in my code that's why it didnt work before!
1

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.

4 Comments

It's not clear how this "doesn't appear to work"; probably you did something else or didn't understand exactly how to refactor your code to use this.'
did i mention i'm pretty new to python, it is most likely the latter
i can't exactly mark this correct as it hasn't worked for me, i'm gonna do more testing on this, wait please.
I GIVE UP IM USING GLOBAL NOW
0

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;
    [...]

4 Comments

Actually the object already has the number as the member self.num
yeah that is what i was confused about, i have n = self.num already tho?
You have the binary input as number in your object, but not the decimal version, since you only print that. Your method two() works with the decimal input, not the binary.
Oh and of course you have to keep two() from using the binary input by default. Or do something like 'n = self.one( self.num )' in two().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.