【问题标题】:Creating a basic calculator in python [duplicate]在python中创建一个基本计算器[重复]
【发布时间】:2017-07-24 23:51:59
【问题描述】:

这是我目前所拥有的:

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)

【问题讨论】:

  • 您是否遇到任何具体错误?
  • 使用 Python 使用^ 计算幂的部分将不起作用。在你的代码中使用**
  • @slackxx,如果您有新错误,请提出新问题,尽管我建议您先尝试调试自己的代码,但不要将它们编辑到您当前的问题中。
  • 从其他地方的 cmets 中,我看到您正在学习 Python 初学者课程。不幸的是,对于使用通用编程语言的初学者项目来说,计算器是一个非常糟糕的选择,因为在这种情况下清理和解析任意输入是一项非常困难的任务。
  • @PadraicCunningham 我无法提出新问题。我需要等待 2 天大声笑

标签: python python-2.7


【解决方案1】:

您使用= 而不是==

应该是这样的

if second =="+":

对所有人都做同样的事情

= 是赋值语句而不是比较

【讨论】:

  • 哦。你是对的!它仍然无法正常工作。 ://
  • @slackxx 您期望脚本返回值,但您的代码只是打印它
  • 当我使用return 时,我会在返回行上看到那条红色的涂鸦线,上面写着'return' outside of function
  • @slackxx 是的,你不能使用没有函数的 return,看看这个链接stackoverflow.com/questions/30664263/…
  • 把它变成一个函数并得到新的错误ValueError: could not convert string to float:
【解决方案2】:

例如,您可以创建一个名为计算器的函数,然后将其与用户输入一起使用。 :

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 当然是用户输入的数字,operator 是首选的操作符。所以基本上这里的函数需要 3 个参数。

【讨论】:

  • 我们目前正在做循环和条件。尚未达到功能。
  • 我明白了。那么基础知识仍然可以适用。您仍然可以做的是询问两个单独的数字,然后询问要使用什么操作并根据可能的选择进行 if 语句来执行操作。
【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多