Reputation: 11
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()
Upvotes: 1
Views: 43
Reputation: 123501
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()
Upvotes: 1