Home > Software engineering >  Error for not defining variables, don't know why that happens
Error for not defining variables, don't know why that happens

Time:10-17

#The number of balls needed to fill a children’s ball pit can be calculated as: the volume of the ball pit divided by the volume of a single ball multiplied by the packing density.

#The packing density is generally 0.75.

#The volume of the ball pit is calculated as: Pi (3.14) * radius2 * height of the ball pit.

#The volume of a ball is calculated as: (4/3) * Pi (3.14) * radius3.

#Write a function to return the volume of the ball pit from two parameters: ball pit radius and ball pit height.

#Write a function to return the volume of a ball from one parameter: ball radius.

#The program should output the number of balls required to fill a pit with a radius of 1m and height of 0.2m using 0.05m balls.

    import math
    packing_density=0.75
    def volume_of_ball_pit():
      radius_pit= float(input("Input the radius of the pit"))
      height_pit= float(input("Input the height of the ball pit"))
      volume_pit= math.pi * radius_pit * height_pit
      return volume_pit

    def volume_of_ball():
      radius_ball= float(input("Input the radius of the ball"))
      volume_ball= (math.pi*(4/3))*radius_ball
      return volume_ball

    def balls_required():
      volume_of_ball_pit()
      volume_of_ball()
      number_of_balls= (volume_pit/volume_ball)*packing_density

    balls_required()

I recieve an error where my variables are not defined, however, I think that it has already been done in the earlier subroutine, any tips?

CodePudding user response:

You have to assign the results of the the functions into variables

def balls_required():
  volume_pit = volume_of_ball_pit()
  volume_ball = volume_of_ball()
  number_of_balls= (volume_pit/volume_ball)*packing_density

CodePudding user response:

the variables can not see each other in different def (functions) , unless you make them global :

make your variables global in the beginning of the code :

  global radius_pit,height_pit,volume_pit, number_of_balls,

CodePudding user response:

First of all, the volume of a sphere (here, ball) is calculated with the formula: (4/3) * pi * (r^3) and that of the pit (I suppose, a cylinder) is calculated with: pi * (r^2) * h

This is what the actual code should look like:

import math
packing_density=0.75
def volume_of_ball_pit():
    radius_pit= float(input("Input the radius of the pit"))
    height_pit= float(input("Input the height of the ball pit"))
    volume_pit= math.pi * radius_pit *radius_pit * height_pit
    return volume_pit

def volume_of_ball():
    radius_ball= float(input("Input the radius of the ball"))
    volume_ball= (math.pi*(4/3))*radius_ball**3
    return volume_ball

def balls_required():
    number_of_balls= (volume_of_ball_pit()/volume_of_ball())*packing_density
    print(number_of_balls)

if __name__ == '__main__':
    balls_required()

This might be the reason why the interpreter is showing that your variables aren't defined.

  • Related