How to solve and equation with inputs in python
The input()
function returns a string value: you need to convert to a number using Decimal
:
from decimal import Decimal
A = Decimal(input("Enter a number for A: "))
# ... etc
But your user might enter something that isn't a decimal number, so you might want to do some checking:
from decimal import Decimal, InvalidOperation
def get_decimal_input(variableName):
x = None
while x is None:
try:
x = Decimal(input('Enter a number for ' + variableName + ': '))
except InvalidOperation:
print("That's not a number")
return x
A = get_decimal_input('A')
B = get_decimal_input('B')
C = get_decimal_input('C')
D = get_decimal_input('D')
E = get_decimal_input('E')
print((((A * 10 ** A) ** 2) ** (B * C)) * D ** E)
Python User input equation
Tack för exempel -- that allowed me to determine what you need.
Yes, this is possible. Read in the equation as a string variable, such as
equation = input("Enter your equation here")
Then when you want to find a value for e, use the Python eval method:
e = eval(equation)
Be very careful with this method: eval()
is powerful, and very discriminating about what it accepts.
- eval is really
dangerous - Security on untrusted
strings
How to solve a math equation if the input is a string in python?
You can split at the '+' sign numbers=input.split('+')
, then remove the '=' form the end, secondNumber=numbers.split('=')
. Afther that write
if operator == '+':
output = numbers[0] + secondNumber[0]
print(output)
Or just output=eval(input.split('=')[0])
How to get equation from input in python
If ths code is for academic purposes and you don't want to involve third party libraries, the eval()
function in Python is built just for this. You can create a function which evaluates a string expression expr
for a value x
as such:
def func(expr, x):
return eval(expr)
input_expr = input('Enter an expression in x: ')
val = 5
print(func(input_expr, val))
Input: x*x*x - x*x + 2
Output: 102
eval()
treats the string passed to it as a right hand side expression and evaluates it. So, when the user-input string 'x*x*x - x*x + 2'
is passed to the function, the statement return eval('x*x*x - x*x + 2')
is equivalent to return x*x*x-x*x+2
.
Note that this means there must be a variable x
in scope, which we handle by wrapping the eval() function in the function eval_expr()
.
A problem would arise however, if the user input something like y*y*y+y*y-2
as y
isn't declared in the scope. If you need to handle such cases, then you would need to write additional code that converts the input string by replacing any alphabetical character in it to x
.
It should be noted that eval
generally isn't considered safe, and if this code is not for academic purposes and needs to be safe, you should consider using python libraries like numexpr or pyparsing. numexpr should be fairly easy to use and would suffice for your application.
Related Topics
Finding the Value of the Min and Max Pixel
How to Get the Name of an Object
How to Iterate Through Cur.Fetchall() in Python
Python Check Multi-Level Dict Key Existence
How to Delete Lines from CSV File Using Python
Matplotlib: Attributeerror: 'Axessubplot' Object Has No Attribute 'Add_Axes'
Numpy: How to Pick Rows from Two 2D Arrays Based on Conditions in 1D Arrays
How to Truncate the Time on a Datetime Object
Only Reading First N Rows of CSV File With CSV Reader in Python
How to Click on an Element from the Dropdown Menu Through Python and Selenium
How to Count the Number of Files in a Directory Using Python
How to Make Print() Accept the User Input in Same Line
Ssl: Certificate_Verify_Failed With Python3
Import Local Module in Jupyter Notebook