# Python - Input Filtering, Calculating with a Variable Amount of Inputs

I've been working on an Aerodynamics calculator using python (which I'm relatively new at), In this program, a value can be calculated based on multiple different inputs ie. a calculates b, b calculates c, or c can find b and b can find a.

To elaborate on the function of this program it will loop through it's logic until it has found all the things it could with the given input(s). The code is pretty long for it's function and that's why I'd like to see if it could be optimized or if I could do anything better. For the input method, the input is a string. The code is as follows:

def Find_Pressure(temp):
pressure = (101.29 * (((temp + 273.1) / 288.08) ** 5.256))
return pressure

def Find_Temp_Alt(alt, ground_temp):
Temp = ground_temp - (0.00649 * alt)
return Temp

def Find_Density(pressure, temp):
density = (pressure / (0.2869 * (temp + 273.1)))
return density

def Find_Alt_Temp(temp, ground_temp):
Alt = ((ground_temp - temp) / 0.00649)
return Alt

def is_Valid(x):
try:
float(x)
return True
except ValueError:
return False

def Parser(ground_temp, temp, alt, pressure, density):
a = t = p = d = False
run = True
Alt = Temp = Pressure = Density = "N/A"
if is_Valid(alt):
Alt = float(alt)
a = True
if is_Valid(temp):
Temp = float(temp)
if Temp <= -273.1:
t = False
else:
t = True
if is_Valid(pressure):
Pressure = float(pressure)
p = True
if is_Valid(density):
Density = float(density)
d = True

if not is_Valid(ground_temp):
print('Enter Ground Temp')
else:
G_T = float(ground_temp)
while run:
run = False
if a and not t:
Temp = Find_Temp_Alt(Alt, G_T)
t = True
run = True

if t and not a:
Alt = Find_Alt_Temp(Temp, G_T)
a = True
run = True

if p and not t:
Temp = ((288.08 * ((Pressure / 101.29) ** (1 / 5.256))) - 273.1)
t = True
run = True

if t and not p:
Pressure = Find_Pressure(Temp)
p = True
run = True

if (p and t) and not d:
Density = Find_Density(Pressure, Temp)
d = True
run = True
if (d and t) and not p:
Pressure = (Density * 0.2869 * (Temp + 273.1))
p = True
run = True

if (d and p) and not t:
Temp = ((Pressure / Density * 0.2869) - 273.1)
t = True
run = True
return Alt, Temp, Pressure, Density


I apricate any help/feedback, thanks in advance!

## 2 Answers

Welcome to Code Review! I'll be adding onto what @Linny has already said about type hints and variable naming. Variable naming is part of python's PEP-8 guidelines (see bottom).

## Magic numbers

You have a lot of magic numbers in your code, which are really conversion constants, but appear with no such explanation.

## Variable names

Since you're writing an aerodynamics calculator, it'd help if you use full names for various variables in your code. For eg. altitude instead of alt, temperature instead of just temp (temp is generally used as a temporary variable in code).

## Ground temperature

Based on the flow of program, I assume that ground_temperature is essential for any calculations. Perhaps check for it at the very beginning and break early in case of invalid checks.

## Optional arguments

From the above, only ground_temperature is needed to call the calculator. Everything else is optional, and can be computed. Perhaps a function which defaults other values to None might suit you better:

def aerodynamic_calculator(
ground_temperature: float,
temperature: float = None,
altitude: float = None,
pressure: float = None,
density: float = None,
):


## Booleans for each parameter

With the above approach, you can just validate for the value itself, without having to keep track of a boolean for those values.

if temperature and not altitude:
altitude = compute_altitude_from_temperature(temperature, ground_temperature)


## PEP-8

In python, it is common (and recommended) to follow the PEP-8 style guide for writing clean, maintainable and consistent code.

Functions and variables should be named in a lower_snake_case, classes as UpperCamelCase, and constants as UPPER_SNAKE_CASE.

Just a couple notes about style

• Variable and function names should be in snake_case
• You should add type hints to display what types of parameters you accept, and what value(s) your functions return.
def find_pressure(temp: float) -> float:
return (101.29 * (((temp + 273.1) / 288.08) ** 5.256))

def find_temp_alt(alt: float, ground_temp: float) -> float:
return ground_temp - (0.00649 * alt)

def find_density(pressure: float, temp: float) -> float:
return (pressure / (0.2869 * (temp + 273.1)))

def Find_Alt_Temp(temp: float, ground_temp: float) -> float:
return ((ground_temp - temp) / 0.00649)

def is_valid(x: str) -> bool:
try:
float(x)
return True
except ValueError:
return False


You don't need to create a variable for a calculation in order to return that calculation. Just return the expression itself.