Ask coding questions

← Back to all posts
User Defined Functions
Kritini2030 (0)

Can you call a variable from one user-defined function into another user defined function?

How?

Answered by themining72 (49) [earned 5 cycles]
View Answer
Comments
hotnewtop
themining72 (49)
# User Radius
def radius():
    radius = input("Enter Radius: ")
    r = float(radius)
    print("Radius =", r)
    return r

# Calculations
def calculate(r):

    area = 3.14 * r * r
    print ("Area =" , area)

# Magic
n = input("If you would like to continue, type yes with a SMALL/LOWERCASE Y.")
while n == "yes":

    calculate(radius())

The above should work, changes:

  • Line 10 removed.
  • Line 9 now accepts a variable.
  • Line 16 removed.
  • Line 17 calls Calculate(r) and gives r as radius()
VulcanWM (2770)

instead of printing the answer try returning it
and the variable will be
var_name = function()