1

What I'm trying to do is ask the user for a type of math operator(Not sure if I used the correct term. I'm new to python.), and use that in place of a math operator in an equation.

Here I tried:

math_thing = int(input('Enter a type of math operator(/,*,+,etc.,)'))
result = 12{0}4.format(math_thing)
print(result)

I was expecting it to ask for a sign, and perform the equation(I was going to add checks to see if it was valid), but what happened was it gave me the error SyntaxError: invalid syntax. I don't really know any other way to do it, and when I looked I thought the .format thing might only work for strings, but I'm not sure.

catasaurus
  • 933
  • 4
  • 20
  • If you change the answer into an int, won't there be a value error? – Random Dec 18 '22 at 02:27
  • No, but I just tried it without. I've been trying to update my post but my wifi keeps going out so I'm having trouble finishing editing in the window of time I have. – Derpy Gamer Dec 18 '22 at 02:30
  • please reference the python documentation to understand how to use the different function available and specific the one you're using, input. Always check the documentation, try the samples, understand the samples, see this site https://pythonexamples.org/python-input/ – MZM Dec 18 '22 at 02:37
  • Your invalid syntax is forgetting quotes, `12{0}4.format(math_thing)` should be `"12{0}4".format(math_thing)`. You can't `.format` a random expression. – ShadowRanger Dec 18 '22 at 02:44

2 Answers2

2

It is not a generally good idea to use eval in python, because the user of the program could input malicious code. So make sure only trusted users are using this program on your computer. Also I got rid of int on the input function as it is not needed:

math_thing = input('Enter a type of math operator(/,*,+,etc.,)')
result = eval('12{0}4'.format(math_thing))
print(result)

Output (when input is *):

48

All that eval does is take a string (a str object) of python code and run it.

Please do keep in mind though that you should never use a program of this type using eval on any system that you are exposing to the internet, because that would make it so anybody any where could run python code on your computer, potentially malicious code.

catasaurus
  • 933
  • 4
  • 20
  • Great! Thank you! I don't know much about python yet and no else I know does either, so I don't think it'll be an issue. I'm just trying to test some things. – Derpy Gamer Dec 18 '22 at 02:32
2

If you're attempting to make a program to perform an equation between two numbers, one solution is to do it in an if statement.

For Example:
operator = input('Enter a type of math operator(/,*,+,etc.,)')
if operator == '*'
    result = firstnumber * secondnumber
if operator == '/'
    result = firstnumber * secondnumber
...and so on
print(result)
Nibrock
  • 41
  • 3
  • I know, but that's inefficient and I'm trying to figure out new ways of doing things instead of boring old if statements. Thanks for your help though! – Derpy Gamer Dec 18 '22 at 16:12