1

One of the functions in my code takes a user inputted number in binary, and another function converts that number to base 10. For some reason my variables are not working right. The function "base2TO10" is not recognizing the user input that is defined as "base2". How can I fix this and let main run all of my functions?

def isBase2():
    flag2 = True

    while(flag2 == True):
        base2 = input("Enter a base 2 number: ")
        try:
            base2 = int(base2 , 2)
            flag1 = False
            return True
        except ValueError:
            flag1 = True
        else:
            flag1 = True
    return base2


**def base2TO10():
    b = base2
    number = 0
    for idx, num in enumerate(b[::-1]):
        base2 += int(num)*(2**idx)
    return number**    


def main():

    if printmenu() == "2":
        askUserValue()
        isBase2()
        base2TO10()
panda67
  • 11
  • 2
  • 3
    It's because your variable `base2` is no longer in scope. You can learn about [function scope here](http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html). – blakev Oct 11 '17 at 21:35
  • 1
    You need to learn about variable scoping rules. You are using local variables. You *could* make your variables all globally scoped, but that is not a good design approach. Rather, you should be taking data as arguments and returning data from your functions. As an aside, and this is a bit of a terminology nitpick, but Python doesn't have variable declarations – juanpa.arrivillaga Oct 11 '17 at 21:37

0 Answers0