-1

I wrote a python script that displays the covered distance of a projectile based on its speed and angle.

However, it doesn't read those two variables that another function (ask_values()) returned from the user.

What prevents the covered_distance() function from reading the 2 variables (theta and velocity) that the user entered in ask_values()?

Here is the program output:

Projectile angle:   10
Velocity:       10
Angle: 10.0 Speed: 10.0
Angle: 0.0 Speed: 0.0 

Distance covered: 0.0

And here is the program itself:

# IMPORT MODULES
from math import *          # Import all methods/attributes from math module
import sys                  # Import sys.exit()

# DECLARE VARIABLES
theta = 0.0                 # theta = angle at which the projectile is launched
velocity = 0.0              # velocity = speed of the projectile
percent_distance_var = 0.0  # percent_distance_var = percentage of the covered distance
max_distance_var = 0.0      # max_distance_var = maximum distance
covered_distance_var = 0.0  # covered_distance_var = covered distance

# Covered distance
def covered_distance(theta_, velocity_, covered_distance_var_):   # Arguments: (theta, speed, covered_distance_var)
    covered_distance_var_ = 0.2041*((velocity_)**2)*sin(theta_)*cos(theta_) # Calculate 0.2041*((velocity)**2)*sin(theta)*cos(theta)
    data = dict(angle=theta_, speed=velocity_, distance=covered_distance_var_)
    print("Angle: {angle} Speed: {speed} \n \nDistance covered_: {distance}".format(**data)) # Doesn't print out the correct variables
    return covered_distance_var_         # Return covered_distance_var

# Ask user for values
def ask_values(theta, velocity):
    theta = float(input("Projectile angle: \t"))
    velocity = float(input("Velocity: \t \t"))
    print("Angle: {} Speed: {}".format(theta, velocity)) # Prints out the correct variables
    return(theta, velocity)


def main():# Main method
    ask_values(theta, velocity) # Ask for values
    covered_distance(theta, velocity, covered_distance_var)

# EXECUTE CODE
if __name__ == "__main__":   # If "__main__" statement
    main()  # Main() method
Larry
  • 1,312
  • 2
  • 15
  • 22

1 Answers1

1

You have to capture the values returned by your functions in main, or else they are discarded and never enter the namespace. When you access the name, it doesn't find anything locally, and looks at the global values for the arguments. So instead of this:

def main():# Main method
    ask_values(theta, velocity) # Ask for values
    covered_distance(theta, velocity, covered_distance_var)

Do:

def main():# Main method
    theta, velocity = ask_values(theta, velocity) # Ask for values
    covered_distance(theta, velocity, covered_distance_var)

Or else the theta and velocity values will correspond to the ones you defined here:

# DECLARE VARIABLES
theta = 0.0                 # theta = angle at which the projectile is launched
velocity = 0.0              # velocity = speed of the projectile

In the global namespace. Assigning values to these variables is pretty useless here if you want to take user input anyway. Python isn't a statically typed language with a notion of variable declaration. Variables "spring into existence" when they are assigned to.

Also, you are probably going to want to print your final results:

print(covered_distance(theta, velocity, covered_distance_var))
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172