我正在尝试创建一个简单的程序来计算百分比变化,但我在打印答案时遇到问题

时间:2017-02-04 19:32:27

标签: python int typeerror

我刚刚开始使用python,而且我以前很少有编码经验。

我已经创建了一个简单的程序来计算百分比变化,但是要使用要打印的行我正在尝试稍微复杂一些的东西。

我目前的代码如下:

num1 = float(input("what is the original number?"))

modi = float(input("How much do you want to increase it by?(please use 0.83/1.56 types rather than percentages)"))

ans = num1 * modi

print(ans,"is",modi,"times"if modi > 1 ("greater than")else "less than",num1)

计算工作正常,但每当我集成更高级版本的打印行时,都会出现错误:

  

TypeError:'int'对象不可调用

我没有看到任何错误,是吗?

1 个答案:

答案 0 :(得分:1)

您错误地使用了if表达式。应该是"greater than" if modi > 1 else "less than"。通常,表达式的语法是:true_value if condition else false_value。正如您的代码段中所写,Python将1 ("greater than")解释为尝试使用参数1()调用函数"greater than",因此抱怨1无法调用。

相关问题