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.
5 Réponses
+ 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 = }')
+ 1
Awesome!
Thank you so much, Bob_Li.
Can I use your code in my project?
+ 1
yes. you could probably even improve it.
+ 1
Thanks, Bob_Li . I'll do it for sure.
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) = }')