Home > front end >  How do I access the value of a variable in one function without calling the entire function in Pytho
How do I access the value of a variable in one function without calling the entire function in Pytho

Time:01-13

The issue I'm having is that in one of my functions ( function range_and_number() ), I'm asking the user for input, and I'm saving this input in a variable called max number. Next I checked to make sure that this value only has numbers in it, and if it does I break the loop.

def range_and_number():
    while True:
        max_number = input("Max Number Possible (No Decimals): ") 
        if max_number.isnumeric() == True:
            print("Your Good")
            max_number = int(max_number)
            break
        else:
            print("Please Re-Type Your Maximum Number: ")

    return max_number

def get_length():
    lives = len(range_and_number)
    
    return lives

def main():
    s = get_length()
    print(f"================= {s} =================")

Issue: I want to access the value of max number in another function ( function get_length() ) because in this function, I want to measure the length of characters in the variable 'max number'.

The error I'm getting is:

lives = len(range_and_number)                                
TypeError: object of type 'function' has no len()

I understand why I'm getting the error, but how do I get around it. How do I access the value stored in max_number without calling the function itself.

P.S. If you have the time, please post as many possible solutions please. Thanks in advance

(What I tried)

  • I tried using a Global Variable and created it at the top of my function
  • I named the Global Variable TE
  • Then, I did TE = max_number
  • After, I tried accessing TE in the second function, but it didn't work

CodePudding user response:

You need to put () after a function name to call it.

You can't get the length of an integer, so you need to convert it to a string in order to call len().

def get_length():
    lives = len(str(range_and_number()))
    
    return lives

CodePudding user response:

The function get_length should call the function range_and_number. Your code should be something like this.

def range_and_number():
    while True:
        max_number = input("Max Number Possible (No Decimals): ") 
        if max_number.isnumeric() == True:
            print("Your Good")
            max_number = int(max_number)
            break
        else:
            print("Please Re-Type Your Maximum Number: ")

    return max_number

def get_length():
    lives = len(str(range_and_number()))
    
    return lives

def main():
    s = get_length()
    print(f"================= {s} =================")

CodePudding user response:

Do you mean that you want to get the number of characters in max_number before casting to int type? If so, you can store it in a global variable as following code. Of course, the range_and_number function must be called before the get_length function is called.

TE = None
def range_and_number(): 
    global TE
    while True: 
        max_number = input("Max Number Possible (No Decimals): ") 
        if max_number.isnumeric() == True: 
            print("Your Good") 
            TE = max_number
            max_number = int(max_number) 
            return max_number
        else: 
            print("Please Re-Type Your Maximum Number: ")

def get_length(): 
    lives = len(TE)
    return lives

def main(): 
    range_and_number()
    s = get_length() 
    print(f"================= {s}")

A better implementation is to get the number of characters in max_number before casting to int in the range_and_number function and return both the number of characters and the value of max_number.

def range_and_number(): 
    while True: 
        max_number = input("Max Number Possible (No Decimals): ") 
        if max_number.isnumeric() == True: 
            print("Your Good") 
            break
        else: 
            print("Please Re-Type Your Maximum Number: ")
    return int(max_number), len(max_number)

def main(): 
    _, s = range_and_number()
    print(f"================= {s}")

CodePudding user response:

You have to call the function and convert it to string:

s = range_and_number().__str__().__len__()

or print the len(max_number) before converting it to int

or you could use logarithm to calculate the length after converting it to intdough it would be unnecessary and inefficient when you already have the length of string.

  • Related