返回一个从a中减去b的表达式

时间:2017-09-20 06:07:35

标签: python

我正在制作计算器作为学校作业。我有一个功能问题:

def subtraction(a, b):
return a, "-", b, "=", a - b

此功能应返回" a - b = c",例如:" 3 - 2 = 1"
当我将文件推送到Git时,我收到一封电子邮件,其中包含有关此特定功能的以下错误:

test_subtraction_3_2:FAILED(2.795 ms)

test_subtraction_floats:FAILED(2.538 ms)

test_subtraction_m_3_m_5:FAILED(2.445 ms)

test_subtraction_random:FAILED(2.598 ms)

我使用print函数测试了这段代码,并在命令提示符下执行了该文件,它完全正常工作。 任何形式的帮助都将受到高度赞赏!

4 个答案:

答案 0 :(得分:1)

尝试使用以下格式:

def subtraction(a, b):
    return "{} - {} = {}".format(a, b, a-b)

答案 1 :(得分:1)

试试这个

def subtraction(a, b):
  return "%s-%s=%s" %(a,b, a - b)

答案 2 :(得分:0)

return a, "-", b, "=", a - b返回元组(a, "-", b, "=", a - b

您需要返回string串联:

return str(a) + " - " + str(b) + " = " + str(a - b)

答案 3 :(得分:-1)

您需要导入数学库以获得其他功能 尝试 - 并且必须明确使用out括号 打印(STR(A)+ STR(b))的