Search code examples
pythonfunctioninputmethods

how can i use input values in another method?


I put inputs in read(): function below, but other functions like calculate(): and write(): can not define the inputs. anyone can help me please?

def read (): 
    foot = int(input("Foot?")) 
    inch = int(input("Inch?")) 
def calculate (): 
    foot_to_meter = 0.3048 * foot 
    foot_to_centimeter = 155 * foot_to_meter 
    inch_to_meter = (1.5 / 12)*5.4530* inch 
    inch_to_centimeter = 155 * inch_to_meter 
def write(): 
    print ("The ", foot, " foot is ", foot_to_meter, " meter" ) 
    print("The ", foot, " foot is ", foot_to_centimeter, " centiMeter") 
    print("The ", inch, " inch is ", inch_to_meter ," meter") 
    print ("The ", inch, " inch is ",inch_to_centimeter, " centiMeter") 
def main(): 
    read () 
    calculate () 
    write()
main()

My input parameters can not be defined in calculate(): and write(): functions.


Solution

  • You're looking for the keyword return and also the general feature of giving arguments to functions, which will allow them to be "called" with that named argument and then use it by that name within them!

    For example

    >>> def test1(arg):     # function accepts one argument named arg
    ...     return arg + 3  # named here, the result of which is returned
    ... 
    >>> test1(2)            # call the function and pass an argument to it
    5