-1

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
Van1sh
  • 1
  • 2
  • You need to call the function: `range_and_number()`. but it returns a number, which doesn't have a length, so what do you expect `len()` to do with that? – Barmar Jan 13 '23 at 02:04
  • when you tried global variables did you make sure to use the "global" keyword in range_and_number()? See: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Kaelan Mikowicz Jan 13 '23 at 02:06
  • Welcome to Stack Overflow. The question does not make sense as asked, because it is based on a misunderstanding. Variables are not "in" the function in the sense that you are thinking; they **only** have a value **because** the function is called; and that value is **specific to each time** that the function is called. – Karl Knechtel Jan 17 '23 at 04:51
  • Think about it this way: it sounds like you want, for some reason, not to have to call `range_and_number`. But if `range_and_number` does not get called, then **where is the information supposed to come from**? `max_number` depends on the user's input, right? So, at the very least, `input` would have to be called, in order to get the user to input something. And this can only happen if `range_and_number` is called, because that's **where the code is** to call `input`. – Karl Knechtel Jan 17 '23 at 04:55
  • It's also not clear to me what the title is supposed to mean, about "calling the entire function". Why "entire"? That doesn't make any sense. A function either gets called or it doesn't. It's a set of steps to follow. If you only want some of those steps to happen, then **make a different function** that only does those steps. – Karl Knechtel Jan 17 '23 at 04:57

4 Answers4

0

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

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.

Fancy way of doing it:

Y = lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))
range_and_number = lambda f: lambda s: (s.__len__() if (s:=input(s)) and s.isnumeric() else f("Please Re-Type Your Maximum Number: \n"))
print("================= %d ================="%Y(range_and_number)('Max Number Possible (No Decimals): \n'))
  • 1
    Hi. Could you please explain why you are recommending `x.__str__().__len__()` rather than `len(str(x))`? – Stef Jan 16 '23 at 11:21
  • There is no apparent difference between `len()` and `__len__()` the `len` function will just call the `__len__`(same with string,repr,unicode) method on your string object at the end. I just used to writing it that way so i don't have to navigate to inside of the parentheses, it's also less operations at the end of the day with string function since it does some [extra checking](https://docs.python.org/3/library/stdtypes.html#str). –  Jan 16 '23 at 12:11
0

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'.

  • I tried using a Global Variable and created it at the top of my function

Don't use a global variable. Use a variable in function main.

def range_and_number():
    while True:
        max_number_as_string = input("Max Number Possible (No Decimals): ")
        try:
            max_number = int(max_number_as_string)
            print("You're good.")
            return max_number
        except ValueError:
            print("Please Re-Type Your Maximum Number.")

def get_length(n):
    return len(str(n))

def main():
    max_number = range_and_number()
    lives = get_length(max_number)
    print('Lives:      {}'.format(lives))
    print('Max number: {}'.format(max_number))

if __name__ == '__main__':
    main()

Output:

Max Number Possible (No Decimals): hello
Please Re-Type Your Maximum Number.
Max Number Possible (No Decimals): 127.5
Please Re-Type Your Maximum Number.
Max Number Possible (No Decimals): 127
You're good.
Lives:      3
Max number: 127
Stef
  • 13,242
  • 2
  • 17
  • 28
-1

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} =================")