用python创建基本计算器

用python创建基本计算器,python,python-2.7,Python,Python 2.7,这就是我到目前为止所做的: 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 == "/":

这就是我到目前为止所做的:

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"
第一个和第三个参数应该是双浮点数。我不确定操作符应该如何表示,所以我将它命名为“second”,并将其设置为字符串。我不知道我应该如何得到这些计算结果。我的if/elif/else语句是否错误?我应该使用“打印”或“返回”进行实际计算吗

这是测试文件的一个示例:

 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)

您正在使用
=
而不是
=

所以应该是这样

if second =="+":
对所有人都一样


=
是一种赋值语句,而不是比较

例如,您可以创建一个名为calculator的函数,然后将其用于用户输入:

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
x和y当然是用户输入的数字,运算符是选择的运算符。因此,这里的函数基本上有3个参数。

这是我的解决方案:

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)

您是否遇到了任何特定错误?使用Python使用
^
计算功率的部分将不起作用。在您的代码中使用
**
。@slackxx,如果您有新错误,请提出新问题,不要将其编辑到当前问题中,尽管我建议您在调试之前尝试调试自己的代码。从您在其他地方的评论中,我看到您正在学习Python初学者课程。不幸的是,对于一般编程语言的初学者项目来说,计算器是一个非常糟糕的选择,因为在这种情况下,清理和解析任意输入是一项非常困难的任务。@PadraicCunningham我不能提出新问题。我需要等两天,呵呵。你说得对!但它仍然不起作用/@slackxx您希望脚本返回一个值,但您的代码只是在打印它。当我使用
return
时,返回行上有一条红色的潦草线,上面写着
'return'在函数之外
@slackxx是的,您不能使用没有函数的返回,只要看看这个链接,它就变成了一个函数,得到了新的错误
ValueError:无法将字符串转换为float:
我们目前正在做循环和条件语句。我知道了,我还没找到函数。好吧,基本原理仍然适用。您仍然可以做的是询问两个单独的数字,然后询问要使用的操作,并根据可能的操作选择生成if语句。