为什么" TypeError:' undefined'对象不可调用"上来?

时间:2014-12-01 00:53:25

标签: python

subtotal = input("What's the Subtotal?") #input of dinners subtotal
tax_input = input("What's the tax there?") #local tax
tip_input = .20 #average tax is 20%
tax = subtotal * tax_input #tax
tip = subtotal * tip_input #tip
total = subtotal + tax + tip #totalling
print "Your %s was " + tax (tax)
print "Your %s was " + tip (tip)
print "Your %s is " + total (total)

错误“TypeError:'undefined'对象不可调用”是什么意思?还有,我的代码好吗?我知道不是使用两个税,而是可以改变价值,但这对我来说似乎很奇怪......

3 个答案:

答案 0 :(得分:1)

按如下方式更改打印件应该有帮助:

print("Your tax was {} ".format(tax))
print("Your tip was {}".format(tip))
print("Your total is {}".format(total))

您的代码不起作用,因为tax,tip和total不是函数或可调用对象。他们只是数字。

答案 1 :(得分:1)

你想做那样的事吗?

subtotal = input("What's the Subtotal?") #input of dinners subtotal
tax_input = input("What's the tax there?") #local tax
tip_input = .20 #average tax is 20%
tax = subtotal * tax_input #tax
tip = subtotal * tip_input #tip
total = subtotal + tax + tip #totalling
print "Your tax was %s" %tax
print "Your tip was %s" %tip
print "Your total is %s" %total

代码中存在的问题是

print "Your %s was " + tax (tax)

tax不是一种方法,因此您无法调用它。您的taxintfloat,因此您需要将其设为要连接的字符串,方法是执行此操作

print "Your tax was %s" %tax

print "Your tax was " + str(tax)

答案 2 :(得分:0)

您正试图将税,小费和总数称为功能。它们是变量,而不是函数。如果您想将字符串税,小费和总数替换为各自的"标题"你需要采用一种方法,使用可迭代的键/值哈希(python方便简单,但这是另一个讨论)。

相反,您可以简单地执行以下操作:

`
print "Your tax was", tax
print "Your tip was", tip
print "Your total was", total
`

事实是你的代码很糟糕。我的也是。它可能总是很糟糕。习惯于定期回顾你之前编写的代码,并且一开始就讨厌编写这些可怕的,可怜的代码。