0

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.

4
  • 6
    Unless you use global variables (which you shouldn't), read has to return the two values so they can be passed as arguments to calculate and write. Commented Apr 18, 2024 at 19:53
  • Similarly, your write function cannot use any of the variables set in the calculate function for the same reason Commented Apr 18, 2024 at 19:55
  • Mandatory link to Ned Batchelder Commented Apr 18, 2024 at 19:57
  • @mkrieger1 There are no syntax errors. (There are NameErrors, but those are runtime errors, not syntax errors.) Commented Apr 18, 2024 at 20:21

3 Answers 3

2

You would have to refer to the global variables inside the functions, in order to mutate them:

# Globals
foot = 0
inch = 0
foot_to_meter = 0
foot_to_centimeter = 0
inch_to_meter = 0
inch_to_centimeter = 0

def read():
    global foot, inch
    foot = int(input("Foot?"))
    inch = int(input("Inch?"))

def calculate():
    global foot, inch, foot_to_meter, foot_to_centimeter, inch_to_meter, inch_to_centimeter
    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, "meters")
    print("The", foot, "foot is", foot_to_centimeter, "centimeters")
    print("The", inch, "inch is", inch_to_meter, "meters")
    print("The", inch, "inch is", inch_to_centimeter, "centimeters")

if __name__ == "__main__":
    read()
    calculate()
    write()

A better way to write this would to return the inputs from the previous function and pass them into the write() function and compute the values inside of a formatted f-string:

def inches_to_meters(inches):
    return inches * 2.54e-2

def inches_to_centimeters(inches):
    return inches_to_meters(inches) * 100

def feet_to_meters(feet):
    return inches_to_meters(feet * 12)

def feet_to_centimeters(feet):
    return inches_to_centimeters(feet * 12)

def read():
    feet = int(input("Feet?"))
    inches = int(input("Inches?"))
    return (feet, inches)

def write(feet, inches):
    print(f"{feet} feet => {feet_to_meters(feet):.4f} meters")
    print(f"{feet} feet => {feet_to_centimeters(feet):.4f} centimeters")
    print(f"{inches} inches => {inches_to_meters(inches):.4f} meters")
    print(f"{inches} inches => {inches_to_centimeters(inches):.4f} centimeters")

if __name__ == "__main__":
    feet, inches = read()
    write(feet, inches)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

FYI: my terminology is not great since I don't use them often (I am new).

The scope of your variable Foot and Inch is only inside the function read. (So to speak local variables).

This means that other functions can not access those variables and do the mathematic operations needed.

What you need to do is to return those variables to the main program and then flow them into your other functions.

Variables that are declared in the main program and not sub-procedure (functions etc) are called global variables by the way.

So an example is this:

def myFunction():
    variable1 = "Hello World"
    return variable1

def printFunction(formalParameter):
    print(formalParameter)

mainVariable = myFunction
printFunction(mainVariable)

now the formalParameter is a placeholder, or called formal parameter. What you actually flow in (this case mainVariable) is called the actual parameter. (Correction if I am wrong thank you!)

Be very careful with the order you declare them.

If you mix up the order, the program will not work!

For example

def function(a,b):
    #the rest of the code

function(b, a)

This will cause you errors. So again, be careful.

Also there is a nice guide on this by geeks for geeks.

Python Scope of Variables

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.