r/AskProgramming • u/ice_or_flames • May 20 '26
Python Is this a good way to build a math program?
degree = int(input("Of what degree is your function?[0-51]: "))
degree_value = degree
function = ""
constant = 97
while degree >= 0:
if degree > 1:
function = function + "" + chr(constant) + "(x^" + str(degree) + ") + "
elif degree == 1:
function = function + "" + chr(constant) + "(x) + "
elif degree == 0:
function = function + "" + chr(constant)
degree = degree - 1
constant = constant + 1
if constant == 123:
constant = 65
print(function)
constant = 97
constants = []
degree = degree_value
while degree >= 0:
new_constant = (float(input(chr(constant) + " = ")))
constants.append(new_constant)
degree = degree - 1
constant = constant + 1
if constant == 123:
constant = 65
number_of_terms = len(constants)
function = ""
location = 0
degree = degree_value
while number_of_terms > 0:
if constants[location] == 0 and degree == 0:
function = function + "0"
elif constants[location] == 0:
function = function
elif constants[location] != 1:
if degree > 1:
function = (function + str(constants[location]) + "x^"
+ str(degree) + " + ")
elif degree == 1:
function = function + str(constants[location]) + "x + "
elif degree == 0:
function = function + str(constants[location])
elif constants[location] == 1:
if degree > 1:
function = function + "x^" + str(degree) + " + "
elif degree == 1:
function = function + "x + "
elif degree == 0:
function = function + str(constants[location])
number_of_terms = number_of_terms - 1
location = location + 1
degree = degree - 1
print("f(x) = " + function)
derivative = ""
location = 0
degree = degree_value
while degree >= 1:
if (constants[location] == 0 and degree == 1):
derivative = derivative + "0"
elif (constants[location] * degree) != 1 and (constants[location] * degree) != -1:
if degree > 2:
derivative = derivative + str(constants[location] * degree) + "x^" + str(degree - 1) + " + "
elif degree == 2:
derivative = derivative + str(constants[location] * degree) + "x + "
elif degree == 1:
derivative = derivative + str(constants[location])
elif (constants[location] * degree) == 1 or (constants[location] * degree) == -1:
if degree >= 3:
derivative = derivative + "x^" + str(degree - 1) + " + "
elif degree == 2:
if constants[location] > 0:
derivative = derivative + "x + "
else:
derivative = derivative + "-x + "
elif degree == 1:
if constants[location] > 0:
derivative = derivative + "1"
else:
derivative = derivative + "-1"
degree = degree - 1
location = location + 1
print("f'(x) = " + derivative + "\n")
if derivative == "0":
print("f'(x) cannot equal zero.")
quit()
print("Newton-Rhapson method:")
finished = False
while not finished:
x = float(input("Initial guess = "))
iterations = int(input("How many iterations?: "))
function = ""
derivative = ""
x_n = 1
number_of_constants = len(constants) - 1
location = 0
while number_of_constants >= 0:
if number_of_constants > 0:
function = function + str(constants[location]) + " * x ** " + str(number_of_constants) + " + "
if number_of_constants > 1:
derivative = derivative + str(constants[location] * number_of_constants) + " * x ** " + str(number_of_constants - 1) + " + "
if number_of_constants == 1:
derivative = derivative + str(constants[location])
if number_of_constants == 0:
function = function + str(constants[location])
number_of_constants = number_of_constants - 1
if number_of_constants >= 0:
location = location + 1
print("x0 = " + str(x))
while x_n <= iterations:
try:
x = x - (eval(function))/eval((derivative))
print("x" + str(x_n) + " = " + str(x))
x_n = x_n + 1
except:
print("f'(x0) cannot equal zero.")
quit()
answer = input("Do you want to do another approximation?[y/n]: ")
if answer == "n":
finished = True
0
Upvotes
4
u/theelevators13 May 21 '26
I actually think this is both horrible and great lol
From a general programming perspective, this code feels like a mess.
Too much state manipulation, complex and unclear logic, deep nested if statements.
The fact that you did this and it works? Fucking great. The rest is really just clean up and maintenance.
My recommendations from here would be dependent if you like object oriented programming or functional programming.
For OOP (object oriented): I’d look into shoving some of the same type of logics into some classes. The function variable and degree are perfect for class properties.
Although, I’d argue that this logic fits functional programming more and you could benefit from a simple class and a state machine.
Think small units of compacted work that together make a flow. The function variable to me is screaming to be used in a state machine.
2
u/Outside_Complaint755 May 20 '26
A few notes:
Instead of building out a string in each loop by adding to the previous string on every iteration append each segment to a list and then build the final string at the end, although you probably won't be able to take direct advantage of the
.join()method as you will probaly want to handle negative coefficients as a subtraction instead of "+ -8x"I really don't understand the point of
which you do in each loop, including the one where you get coefficients from the user. But it has been 20+ years since I was doing calculus and differential equations so maybe its some setup I have forgotten.
You shouldn't use a naked
exceptas it may catch a different kind of error that should crash, like a RuntimeError or KeyboardInterrupt. You should always specify the type of exception you expect to catch, which I guess here would be a ZeroDivisionError or a ValueError.2
u/BobbyThrowaway6969 May 21 '26
You can make it more efficient in several ways, the first one is to replace all that branching with lookups and caching
1
u/ice_or_flames May 20 '26
I am at a very basic level of python, but I wrote a math program that works as far as I can tell, can you, the experienced programmers, see any problems in how I wrote it? Can I make it more efficient?
4
u/JackTradesMasterNone May 20 '26
The general advice I would give is to look at cyclomatic complexity. How deep is your logic? How many nested structures do you have? Functions should be specific and do one thing. They should be readable at first glance and you should be using comments to document what they do.
2
u/marrsd May 21 '26
You need to paste this in a format we can understand. I've reformatted the code for myself but for some reason Reddit hangs when I try to paste anything here.
Anyway, your use of
whileloops is clumsy. You're mutating degree as part of your while loop and then resetting it todegree_value, which you're using to store the number of degrees the function should have.A cleaner way to do this is to replace your
whileblock with:Notice how I'm not managing variables. I've stopped mutating
degreeand I'm not mutatingconstantany more either so there's no danger of me forgetting to reset it.The last thing that is still being mutated in that block is
function. Can you find a different way to implement that block such that you don't need to maintain the state offunctionthroughout your programme?You need to get out of the habit or reusing the same variable for multiple things. This gets very unmanageable very quickly.
Suppose I suggested you define a function called
make_polynomial, that takesdegreeas an argument and returnsfunctionas an output. How would you write the body? How would that help you solve the problems I described above?1
u/Glove_Witty May 20 '26 edited May 23 '26
Edit: AI slop redacted.