1
votes
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)

every time i end the loop my output of the list shows the items i named and their price, but i can not get the total to print out i only get an error message

TypeError: unsupported operand type(s) for +: 'int' and 'str' on line 14

I just started coding very recently and I'm not too sure what to do

4
Why are you using raw_input and input. If you are using python 3, just use input - whackamadoodle3000
I believe the loop should be in the main function. If so please indent the code accordingly. If it isn't, then you can get rid of the main function as it does not do anything. - Xero Smith
@Silencer, I believe the original code was meant for python2. The edited version is strictly python3 thus answers will loose some of the nuance for python2. raw_input() implicitly converts user inputs to strings. While input() accepts only numbers. Passing an input that cannot be interpreted as a number to input() raises NameError. Please redit the question to reflect the op's original meaning. - Xero Smith
@XeroSmith When I post the change, some I found someone has changed it. But I cannot cancle it, until it was peer reviewed.... I get it back - Kinght 金

4 Answers

1
votes

You're making two mistakes here.

1) input() will return the string of whatever you type. So when you add in addItemP, which is the cost, its only a string and not a an int. so sum(addItemP) won't work. convert it to int using int(addItemP)

2) You aren't using a list. Otherwise the total will only have the cost of the last item.

This should work.

def main():
    import math
#the loop
PriceList=[]
choice=str(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?")
    PriceList.append(int(additemP))
    print(addItem + "---------------------$" + additemP)
    choice=str(input("Will you add an item to the list? Just say yes or no"))
if choice != "yes":
    quit

total = sum(PriceList)

print(total)
0
votes

You need to use a list.

choice=str(raw_input("Will you add an item to the list? Say 'yes' to add or 'no' once you're done"))
listofprices=[]
while choice== "Yes" or choice== "yes":
    addItem=input("What is the item?")
    additemP=input("How much does that cost?")
    listofprices.append(int(additemP))
    print(addItem + "---------------------$" + additemP)
    choice=str(input("Will you add an item to the list? Just say yes or no"))
    if choice != "yes":
       total = sum(listofprices)
       break
print(total)
0
votes

You need to use list because sum function use loop to add values like

sum([1,2,3,4])

or

 iter=[1,2,3]
 sum(iter)
0
votes

There is an Indentation error at the if statement it suppose to be

If choice !="yes": quit Print(total)