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.
returnthe value rather than just print it (and actually it would probably be a better design to onlyreturnand have the callerprintor do whatever it wants with the returned value).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.return d1?