Python 如何解决此问题;类型错误:';str';对象不可调用";错误?

Python 如何解决此问题;类型错误:';str';对象不可调用";错误?,python,string,user-interface,comparison,callable,Python,String,User Interface,Comparison,Callable,我正在创建一个基本程序,它将使用GUI获取商品的价格,如果初始价格小于10,则从价格中扣除10%,如果初始价格大于10,则从价格中扣除20%: import easygui price=easygui.enterbox("What is the price of the item?") if float(price) < 10: easygui.msgbox("Your new price is: $"(float(price) * 0.1)) elif float(price)

我正在创建一个基本程序,它将使用GUI获取商品的价格,如果初始价格小于10,则从价格中扣除10%,如果初始价格大于10,则从价格中扣除20%:

import easygui
price=easygui.enterbox("What is the price of the item?")
if float(price) < 10:
    easygui.msgbox("Your new price is: $"(float(price) * 0.1))
elif float(price) > 10:
    easygui.msgbox("Your new price is: $"(float(price) * 0.2))

为什么会出现此错误?

您试图将字符串用作函数:

"Your new price is: $"(float(price) * 0.1)
因为字符串文本和括号(…)之间没有任何内容,Python将其解释为一条指令,将字符串视为可调用的,并用一个参数调用它:

>>> "Hello World!"(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
下一行也需要修复:

easygui.msgbox("Your new price is: $" + str(float(price) * 0.2))
或者,使用以下字符串格式:

其中,
{:02.2f}
将被价格计算替换,将浮点值格式化为2位小数的值。

此部分:

“您的新价格是:$”(浮动(价格)

要求python调用此字符串:

“您的新价格是:$”

就像你想要一个函数一样:
函数(一些参数)
这将始终触发错误:

TypeError:“str”对象不可调用

easygui.msgbox("Your new price is: $" + str(float(price) * 0.2))
easygui.msgbox("Your new price is: ${:.2f}".format(float(price) * 0.1))
easygui.msgbox("Your new price is: ${:.2f}".format(float(price) * 0.2))