-1

This is what I have so far:

import sys

first = float(sys.argv[1])
second = str(sys.argv[2])
third = float(sys.argv[3])

if second == "+":
  print first + third
elif second == "-":
  print first - third
elif second == "*":
  print first * third
elif second == "/":
  print first / third
elif second == "^":
  print first ** third
else:
  print "Invalid Operator"

The first and third arguments are supposed to be double floating point numbers. I wasn't sure how the operator is supposed to be represented, so I just named it "second" and set it as a string. I'm confused as to how I'm supposed to actually get the calculations. Are my if/elif/else statements wrong? Am I supposed to use "print" or "return" for actual calculations to be made?

This is an example of a test file:

 def test_add(self):
    output = self.runScript("simple_calc.py", "1", "+", "1")
    result = float(output)
    self.assertAlmostEqual(2.0, result, places=10)
    output = self.runScript("simple_calc.py", "-1", "+", "1")
    result = float(output)
    self.assertAlmostEqual(0.0, result, places=10)
    output = self.runScript("simple_calc.py", "1.0", "+", "-1.0")
    result = float(output)
    self.assertAlmostEqual(0.0, result, places=10)
slackxx
  • 11
  • 2
  • 1
    Is there any specific error you're encountering? – Łukasz Rogalski Oct 23 '16 at 11:20
  • The part which calculates a power using `^` using Python will not work. Use `**` in your code instead. – Hidde Oct 23 '16 at 11:22
  • @slackxx, if you have new errors then ask a new question, don't edit them into your current question although I suggest you try debugging your own code before you do. – Padraic Cunningham Oct 23 '16 at 12:42
  • From your comments elsewhere, I see that you are taking a beginner Python course. Unfortunately, calculators are a very poor choice for beginner projects in a general programming language, as sanitizing and parsing arbitrary input is a very difficult task in this context. – TigerhawkT3 Oct 23 '16 at 13:11
  • @PadraicCunningham I can't make new questions. I need to wait 2 days lol – slackxx Oct 23 '16 at 14:05

3 Answers3

2

your using = instead of ==

so it should be like this

if second =="+":

and do the same for all

= is an assignment statement not comparison

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Oooh. you're right! It's still not working though. :/ – slackxx Oct 23 '16 at 11:28
  • @slackxx you are expecting a return value from your script but your code is just printing it – Pavneet_Singh Oct 23 '16 at 11:33
  • When I use `return` I get that red scribbly line on the return lines that say `'return' outside of function` – slackxx Oct 23 '16 at 11:37
  • @slackxx yeah you can't use return without function , just take a look at this link http://stackoverflow.com/questions/30664263/return-value-from-one-python-script-to-another – Pavneet_Singh Oct 23 '16 at 11:44
  • Turned it into a function and getting new error `ValueError: could not convert string to float:` – slackxx Oct 23 '16 at 12:00
  • @slackxx you need to use some debugging , will help a lot , try printing the value which you are trying to convert into float(where you are getting error) – Pavneet_Singh Oct 23 '16 at 12:03
0

You could for example make a function called calculator and then use it with user inputs. :

def calculator(x, y, operator):
    if operator == "+":
        return x + y
    if operator == "-":
        return x - y
    if operator == "*":
        return x * y
    if operator == "/":
        return x / y

The x and y would of course be numbers user would input, operator is the operator of choice. So basically the function here would take 3 arguments.

JackTheCrab
  • 132
  • 2
  • 11
  • We're currently doing loops and conditionals. Haven't reached functions in yet. – slackxx Oct 23 '16 at 11:29
  • I see. Well the basics can still apply. What you could still do is ask for two separate numbers, then ask what operation to use and make if statements based on possible choices to do the operation. – JackTheCrab Oct 23 '16 at 11:34
0

Here is my solution:

def Operation():    
    while True:
        operation = raw_input('Select operation: \n + = addition\n - = Subtraction\n * = multiplication\n / = Division\n')
        if operation.strip() == '+':
            break
        elif operation.strip() == '-':
            break        
        elif operation.strip() == '*':
            break
        elif operation.strip() == '/':
            break            
        else:
            print "Please select one of the operations"
            continue       
    return operation   

def number():
    while True:    
        try:
            number = int(raw_input("Please enter a number: "))
            break
        except ValueError:
            print "Please enter valid number: "
    return number

num1 = number()
operator = Operation()
num2 = number()

def calculate(num1, operator, num2):
    if operator == "+":
        return num1 + num2
    elif operator == "-": 
        return num1 - num2    
    elif operator == "*": 
        return num1 * num2
    elif operator == "/": 
        return num1 / num2

print calculate(num1, operator, num2)