-1

I went through a lesson of creating a simple calculator using Python, and I'm trying to simplify it. The issue is as follows: I know that it is possible to parse a string (number) into a float using "float()", but I'm trying to parse a addition/subtraction/multiplication/division sign from a string into a float or integer or any other format that would perform the action. Here's a sample of what I'm trying to do:

while True:
    user_input = input("""
        quit - exit program
        add - addition
        sub - subtraction
        mul - multiplication
        div - division
        Please choose function:""")
    actions = ("+-*/")
    user_input_1 = float(input("first number:"))
    user_input_2 = float(input("second number:"))
    operation = user_input_1 float(action) user_input_2
    if user_input == "add":
        action = actions[0]
        answer = operation
        print (answer)

If the user_input is "add" user_input_1 is "5" user_input_2 is "7" then the print(answer) should result in 12

This is just the first part of the code, and I'm getting a syntax error. The issue is most likely the inability to parse the addition sign using "float()". Is there another way of parsing the signs?

CinCout
  • 9,486
  • 12
  • 49
  • 67
  • 1
    Can you show some expected input and output examples? – Will Jul 11 '16 at 10:45
  • *I'm trying to parse a addition/subtraction/multiplication/division sign from a string into a float or integer* isn't very clear – Moses Koledoye Jul 11 '16 at 10:45
  • 1
    Possible duplicate of [Evaluating a mathematical expression in a string](http://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string) – frnhr Jul 11 '16 at 10:47

4 Answers4

3

Even though you should be posting the full traceback, the SyntaxError comes from this line:

operation = user_input_1 float(action) user_input_2

It is neither a valid Python expression nor a statement.

A solution that doesn't involve eval: You can use the operator module and a dictionary from "sign" to the operator itself, as seen in this very basic (and error prone) example:

import operator

operations_dict = {'+': operator.add,
                   '-': operator.sub} # extend to other operators you see fit

a = float(input('first num'))
b = float(input('second_num'))
sign = input('operator')
print(operations_dict[sign](a, b))
>> first num
   1
>> second num
   2
>> operator
   +
>> 3.0
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

You can use eval in the form eval('1 + 2'), which will give you desired result. You can read more on eval here https://docs.python.org/2/library/functions.html#eval

but please keep in mind the following statement

The user can expose hidden values in the program, or call a dangerous function (dangerous_function("/etc/passwd")). Of course in most cases (desktop programs) the user can't do any more than they could do by writing their own python script, but in some applications (web apps, kiosk computers), this could be a risk.

which is from http://lybniz2.sourceforge.net/safeeval.html

Sumit
  • 2,190
  • 23
  • 31
  • 1
    `eval` is generally considered as something you want to avoid as much as you possibly can (especially when relying on input from users), and in this case you definitely can. – DeepSpace Jul 11 '16 at 10:49
  • programmer can put a check for accepting only floating point numbers while taking input – Sumit Jul 11 '16 at 10:50
  • Religious dogma that everyone must follow: "Never mention `eval` without also mentioning **evil** in the same sentence!" Amen. So say we all! It is known. – frnhr Jul 11 '16 at 10:51
  • I added the evil part too. – Sumit Jul 11 '16 at 10:54
  • This answer also doesn't really solve the `SyntaxError` OP is experiencing.,. – DeepSpace Jul 11 '16 at 10:54
  • well this is answer for the first part, that does not support the down voting of answer as there are two part of the question. – Sumit Jul 11 '16 at 10:57
  • 1
    I feel your pain, apparently there are some people who dislike simply suggestion that `eval` exists. – MisterMiyagi Jul 11 '16 at 11:25
  • 1
    @MisterMiyagi Thanks, but I don't see any harm in controlled usage of eval as it is much shorter code and perform less checks than other suggested answers – Sumit Jul 11 '16 at 11:29
1

In your case this should work :

def operation(action): 
    return user_input_1 + action + user_input_2

if user_input == "add":
    action = actions[0]
    answer = operation(action)
    print (eval(answer))

but that's not the best way to do the operations. You can simply add or divide like this or you can construct a dictionary for the operations:

def operation(action): 
    if(action == "add"):
        return user_input_1 + user_input_2
    elif action == "sub":
        return user_input_1 - user_input_2
    elif action == "div":
        return user_input_1 / user_input_2
    # and so on....

answer = operation(user_input)
print (answer)
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
0

I do not recommend using eval() however; setting some rules won't hurt using eval(). This is really basic calculator and its easy to understand.

allowed_characters = "0123456789+-/*= "
print("""
Basic calculator application.
Operators:
    +   addition
    -   subtraction
    *   multiplication
    /   division
Type the desired operation and press ENTER. 
(Example 23 and 46 to multiple
Type 23 * 46 then press ENTER.)
""")
while True:
    data = input("Please enter your calculation: ")
    if data == "q":
        print("Exiting...")
        break
    for s in data:
        if s not in allowed_characters:
            print("Invalid character!")
            quit()
    calculate = eval(data)
    print(calculate)
mtkilic
  • 1,213
  • 1
  • 12
  • 28