Hey guys im need some help with this binary calculator,
you enter the amount of bits or n in and then it calculates the number?
In order for you to understand my problem more I will explain why I am making this,
I asked my dad how big 128 bit code is, so he asked me to work out some number in binary, and the larger n got or bits, the bytes or whatever got hugely bigger. So I decided to make a calculator in order to quicken this process and as a way to practice python. I need someone to explain what this is about, and if my program is going the right way to work it out. Anyway, the problem with the code is that the variables are not being assigned back to the function "main()" so x and n stay the same. Help is very much appreciated, thanks.
Also Im a minor (14) and learning from some books so I am going to need a more explained answer and maybe some advice on the structuring of code and possibly effiency? Again Thanks
# This is a bits to bytes or what ever calculator (tbh I have no idea, im not sure if im sure what im talking about), attempts to enter a negative number might produce false answers, although this has not been tested # The formula I worked out to find how many bits is ((x-n)*2)+(n-2) where x is the number of bits # At the moment this program is only designed to workout the number of what the binary is equal to. def findx(x): verified = False while verified == False: try: int(input("Enter: ")) except NameError: print("A integer is required to proceed.") except TypeError: print("A integer is required to proceed.") except SyntaxError: print("A integer is required to proceed.") else: verified = True return x def findn(x): n = x while n != 1: n /= 2 n += 1 n += 1 return x, n def main(): x = 0 n = 0 findx(x) #woah what wrong here??? I believe it might be something to do with scope or my misunderstanding of the return statement o.0 print(str(x)) print(str(n)) findn(x) print(str(x)) print(str(n)) answer = (x-n*2)+(n-2) print(str(answer)) main() input()
Edited 4 Years Ago by ThePythonNoob