1

When running my program and giving a user input, it only does the last function in the code. I know what is wrong with the code, I am redefining the same function with different arguments. However, I do not know how to change this so that the code will run the function that matches the user input. Here is the code I wrote for the program.

import math
from math import pi


def equation(Lift):
  d = float(input("Input d value: "))
  v = float(input("Input v value: "))
  A = float(input("Input A value: "))
  CL = float(input("Input CL value: "))

  Lift = .5 * d * (v ** 2) * A * CL 

  a = ["Lift = ", Lift, "Newtons"]
  for i in range (3):
    print(a[i], end =" ")

def equation(VCylinder):
  r = float(input("Input r value: "))
  h = float(input("Input h value: "))

  VCylinder = pi * (r ** 2) * h

  a = ["Volume of Cylinder =", VCylinder, "m³ (meters³)"]
  for i in range(3):
    print(a[i], end =" ")

def equation(Exponentdx):
  n = int(input("Input n value: "))

  u = n
  v = n - 1

  a = ["dx =", u,"x","**", v, " "]
  for i in range(5):
    print(a[i], end =" ")


def Main():
  print("Currently only compatible with base metric units (meters, 
kilograms, etc.)")
  Equation = input("Enter desired equation: ")
  equation(Equation)

Main()
  • 1
    You only have one `equation`, the last one you defined; the others have been shadowed by subsequent definitions. – jonrsharpe Jan 23 '19 at 17:31
  • I know this, however I don't know how I need to change my code to make it work as desired. – John Bailey Jan 23 '19 at 17:32
  • 1
    You have to change to: `def Exponentdx():` and a `condition` like `if Equation == 'Exponentdx':` – stovfl Jan 23 '19 at 17:37
  • do I need to write that condition for every function? or is there a more compact way of handling that? – John Bailey Jan 23 '19 at 17:38
  • 1
    It may be worth reviewing how to [write functions in python](https://www.tutorialspoint.com/python/python_functions.htm). You seem to be confusing naming/defining a function with how to pass parameters/arguments into a function. The way you've written yours, each function is expecting an argument to be passed to it, that argument being the string you assigned to the variable `Equation` – G. Anderson Jan 23 '19 at 17:44
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –  Jan 23 '19 at 18:05
  • Thank you G. Anderson , I will check it out, – John Bailey Jan 23 '19 at 18:05

1 Answers1

1

You need to give each equation function a unique name. This will allow you to build a dictionary mapping an identifier to each one. In addition, you'll need to get an argument value to pass the chosen function before it is called.

Here's an example of doing those all those things:

import math
from math import pi


def equation1(Lift):
  d = float(input("Input d value: "))
  v = float(input("Input v value: "))
  A = float(input("Input A value: "))
  CL = float(input("Input CL value: "))

  Lift = .5 * d * (v ** 2) * A * CL

  a = ["Lift = ", Lift, "Newtons"]
  for i in range (3):
    print(a[i], end =" ")

def equation2(VCylinder):
  r = float(input("Input r value: "))
  h = float(input("Input h value: "))

  VCylinder = pi * (r ** 2) * h

  a = ["Volume of Cylinder =", VCylinder, "m³ (meters³)"]
  for i in range(3):
    print(a[i], end =" ")

def equation3(Exponentdx):
  n = int(input("Input n value: "))

  u = n
  v = n - 1

  a = ["dx =", u,"x","**", v, " "]
  for i in range(5):
    print(a[i], end =" ")

# Build dictionary mapping an equation identifer to equation functions.
equations = {
    '1': equation1,
    '2': equation2,
    '3': equation3,
}

def Main():
  print("Currently only compatible with base metric units (meters, kilograms, etc.)")
  eq_id = input("Enter desired equation function (1, 2, or 3): ")
  eq_arg = float(input("Enter value of argument to pass equation function: "))
  equations[eq_id](eq_arg)  # Call selected equation with argument.

Main()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thank you! this seems to have solved my problem, I didn't think about building a dictionary with it – John Bailey Jan 23 '19 at 17:53
  • John: Glad to hear it solved the problem. Note, to keep things simple I didn't add any error checking/handling, which is something you'll probably also want to do. See the question [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) for ideas on how to do that sort of thing. – martineau Jan 23 '19 at 17:58
  • I wrote this into my code, however it is tripling the output, probably a minor issue I can solve, but helpful nonetheless! Ill check that post as well – John Bailey Jan 23 '19 at 18:04
  • John: Don't understand how the output could become tripled as I don't see that when I run the code. Unrelated suggestion: You might want to make each equation function prompt the user for what is now passed as an argument to it. This would be more flexible because it would allow you to have equation functions that process more than a single `float` argument. – martineau Jan 23 '19 at 18:13
  • I dont know why it is tripling either, but it might be because I am using repl.it as my environment. And I will give that suggestion a shot – John Bailey Jan 23 '19 at 18:31