def main():
import math
#the loop
choice=str(raw_input("Will you add an item to the list? Say 'yes' to add or 'no' once you're done"))
while choice== "Yes" or choice== "yes":
addItem=input("What is the item?")
additemP=input("How much does that cost?")
print(addItem + "---------------------$" + additemP)
choice=str(raw_input("Will you add an item to the list? Just say yes or no"))
if choice != "yes":
quit
total = sum(additemP)
print(total)
我每次结束循环我的列表中的输出显示我命名的项目及其价格
,但我不能得到总打印出来,我只得到一个错误信息打印总和为while循环蟒蛇
TypeError: unsupported operand type(s) for +: 'int' and 'str' on line 14
我刚开始编码最近,我也不太清楚该怎么做
你为什么要使用的raw_input和输入退出 打印(总)。如果您使用的是Python 3,只需使用输入 –
我相信''loop''应该在''main''函数中。如果是这样,请相应缩进代码。如果不是,那么你可以去掉''main''函数,因为它什么都不做。 –
@Silencer,我相信**原来的代码是为python2 **而设计的。 **编辑版本是严格的python3 **,因此答案会松动python2的一些细微差别。 ''raw_input()''将用户输入隐式转换为字符串。而''input()''只接受数字。将不能被解释为数字的输入传递给''input()''引发''NameError''。请重新提出问题以反映op的原始含义。 –