-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)
5
  • 1
    Is there any specific error you're encountering? Commented Oct 23, 2016 at 11:20
  • The part which calculates a power using ^ using Python will not work. Use ** in your code instead. Commented Oct 23, 2016 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. Commented Oct 23, 2016 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. Commented Oct 23, 2016 at 13:11
  • @PadraicCunningham I can't make new questions. I need to wait 2 days lol Commented Oct 23, 2016 at 14:05

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

Oooh. you're right! It's still not working though. :/
@slackxx you are expecting a return value from your script but your code is just printing it
When I use return I get that red scribbly line on the return lines that say 'return' outside of function
@slackxx yeah you can't use return without function , just take a look at this link stackoverflow.com/questions/30664263/…
Turned it into a function and getting new error ValueError: could not convert string to float:
|
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.

2 Comments

We're currently doing loops and conditionals. Haven't reached functions in yet.
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.
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.