【问题标题】:I am trying to create a simple program to calculate percentage change, but I am having issues with printing the answer我正在尝试创建一个简单的程序来计算百分比变化,但我在打印答案时遇到了问题
【发布时间】:2017-06-22 00:46:38
【问题描述】:

我刚刚开始使用 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' 对象不可调用

我看不出有什么问题,你可以吗?

【问题讨论】:

    标签: python int typeerror


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 2017-05-29
      • 2021-12-16
      • 2021-11-12
      • 1970-01-01
      • 2021-05-26
      • 2021-12-30
      相关资源
      最近更新 更多