You can use functools.cache() and it will make it so that if the function has already been called, it stores the original value it returned.
import functools
@functools.cache
def functionA():
a = input("Give a number: ")
b = str(a)
b = len(b)
return b
def functionB():
c = functionA()
print(str(c))
functionA()
functionB()
This technically solves the problem, but I do not think it’s a good use case for functools.cache. Caching functionA means you’ll never be able to call it again in the same program and get different inputs. For example, you might expect the following program to ask for input twice:
import functools
@functools.cache
def functionA():
a = input("Give a number: ")
b = str(a)
b = len(b)
return b
def functionB():
c = functionA()
print(str(c))
functionB() # First time
functionB() # Second time
However, it instead only asks for input once because of functools.cache.
Instead, I’d recommend simply removing the line functionA() at the bottom of the program:
def functionA():
a = input("Give a number: ")
b = str(a)
b = len(b)
return b
def functionB():
c = functionA()
print(str(c))
functionB()
Since the body of functionB contains c = functionA(), this will still call functionA, and it will do so only once. (The function used to get called twice because the first functionA() happened at the bottom and the second functionA() happened within functionB().)