0

First degree equation

I'm trying to do something simple and easy to calculate the value of x from a first degree equation. I have this script: a = int(input('Type the value a: ')) symb = input('Type the symbol: ') b = int(input('Type the value b: ')) c = int(input('Type the value c: ')) x = int() if symb == '+': n1 = (a*x + b == c) n1 = (x == (c - b)/a) n1 = (x) There's a lot of things out here, but this is the main idea. Does anyone know a better way to do it like that? Thanks in advance.

1st Jan 2023, 11:01 AM
Alexandre Leal de Medeiros Martins Moura
Alexandre Leal de Medeiros Martins Moura - avatar
5 odpowiedzi
+ 2
Alexandre Leal de Medeiros Martins Moura try this: # sample input: # 5 8 9 # + a, b, c = map(int, input().strip().split(' ')) op = input() print('inputs:') print(f'{a = }, {b = }, {c = }, {op = }') print('\nsolution:') if op=='+': x = (c - b)/a print(f'{a}x + {b} = {c}\n{x = }') if op=='-': x = (c + b)/a print(f'{a}x - {b} = {c}\n{x = }') if op=='/': x = c * b /a print(f'{a}x / {b} = {c}\n{x = }') if op=='*': x = c /b / a print(f'{a}x * {b} = {c}\n{x = }')
1st Jan 2023, 11:46 AM
Bob_Li
Bob_Li - avatar
+ 1
Awesome! Thank you so much, Bob_Li. Can I use your code in my project?
1st Jan 2023, 12:19 PM
Alexandre Leal de Medeiros Martins Moura
Alexandre Leal de Medeiros Martins Moura - avatar
+ 1
yes. you could probably even improve it.
1st Jan 2023, 12:20 PM
Bob_Li
Bob_Li - avatar
+ 1
Thanks, Bob_Li . I'll do it for sure.
1st Jan 2023, 12:21 PM
Alexandre Leal de Medeiros Martins Moura
Alexandre Leal de Medeiros Martins Moura - avatar
0
Alexandre Leal de Medeiros Martins Moura if you're interested in algebra, you could try sympy https://www.sympy.org/en/index.html from sympy import symbols,expand,factor x, y = symbols('x y') exp = 2*x + y print(f'{exp = }') print(f'{exp + 1 - x = }') exp2 = x*exp print(f'{x*exp = }') exp3 = expand(x*exp) print(f'{expand(x*exp) = }') print(f'{factor(x**2 + 2*x*y) = }')
2nd Jan 2023, 2:22 AM
Bob_Li
Bob_Li - avatar