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))

main()
input()
``````

Here is a stronger code to read the number from user input.

``````def findx(): # This function asks an integer from the user, it does not need a parameter.
while True: # This means 'repeat indefinitely' (unless a break or return or raise statement is met)
try:
z = int(input("Enter: "))
except (ValueError, TypeError): # Your code can handle more than one exception at the same time
print("A integer is required to proceed.")
else:
if z <= 0: # the code can handle the negative case
print("A positive integer is required to proceed.")
else:
return z # exit the function findx(), returning a value

def main():
x = findx() # this is how you catch the value returned by findx()
print("x = ", x) # python converts x to str for you. No need to write str(x)

main()
input()
``````

I don't understand exactly what you want to do with this number x. Your
formula

((x-n)*2)+(n-2)

looks false if we are speaking about number of bits.

The number of bits of an integer usually means the number of digits it takes when it is written in base 2. For example the number 2387 can be written 100101010011 in base two, and one says that the number of bits of 2387 is 12 because there are twelve 0 and 1 in base 2. Is it what you mean ?

I over thought this a bit too much and in the process I made some bad mistakes >.< I remade the program in a minute or two and it works. However you have raised some concepts that I was unaware of, somehow I never picked up that you had to get the variable from the function like that. I got a new book called more python programming for the absolute begginer through the post today so I should be able to progress my learning now that issue has been solved ;) Thanks a bunch