Skip to main content
 首页 » 编程设计

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

2025年02月15日8rubylouvre

我刚刚开始使用 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' object is not callable

我看不出有什么不妥,你呢?

请您参考如下方法:

您错误地使用了 if表达。应该是"greater than" if modi > 1 else "less than" .一般来说,表达式的语法是:true_value if condition else false_value .正如您的代码片段中所写,Python 解释 1 ("greater than")作为调用函数 1() 的尝试使用参数 "greater than" ,因此投诉1不可调用。