如图所示,我编写了此代码,并为CASH和TOTAL分配了值。我无法理解的是为什么我得到..... " Traceback(最近一次调用最后一次): 文件" C:\ Python27 \ Checkout Counter2.py",第29行,in 变化=现金 - 总计 TypeError:不支持的操作数类型 - :' str'和' str'"
我已经尝试了多种方法来完成这项工作,但我发现它与找到总数之间没有任何区别。
print "Welcome to the checkout counter! How many items are you purchasing today?"
#NOI is number of items
NOI = int(raw_input())
productlist = []
pricelist=[]
for counter in range(NOI):
print"Please enter the name of product", counter+1
productlist.append(raw_input())
print"And how much does", productlist[len(productlist)-1], "cost?"
pricelist.append(float(raw_input()))
if pricelist[len(pricelist)-1] < 0:
pricelist.pop()
productlist.pop()
len(productlist)-1
len(pricelist)-1
print "Your order was:"
subtotal=0.00
for counter in range(NOI):
print productlist[counter],
print "$%0.2f" % pricelist[counter]
subtotal += pricelist[counter]
total = "$%0.2f" % float(subtotal + (subtotal * .09))
print "Your subtotal comes to", "$" + str(subtotal) + ".", " With 9% sales tax, your total is " + str(total) + "."
print "Please enter cash amount:"
cash = raw_input()
while True:
change = cash - total
if cash < total:
print "You need to give more money to buy these items. Please try again."
else:
print "I owe you back", "$" + float(change)
答案 0 :(得分:1)
“raw_input”将始终返回一个字符串(即使您输入3或3.5)
因此你必须:
cash = float(cash)
total = float(total)
编辑:此外,当你这样做:
total = "$%0.2f" % float(subtotal + (subtotal * .09))
total也将是一个字符串,这就是为什么你还必须将它转换为float。
希望它有所帮助。