Python: TypeError: can't multiply sequence by non-int of type str
Question:
So, I was making a text-based “shopping” program, and I came across an error that didn’t quite make sense to me. The error message was this:
TypeError: can't multiply sequence by non-int of type str
Here is a code snippet of where I think the error is located (Comment above line where I believe error to be located):
def buy():
print "nType in a store item to buy."
find_item = raw_input(prompt)
if find_item in store_items:
print "Amount?"
amount = raw_input(prompt)
if amount:
#Error in next line?
store_rec.append(store_items[find_item] * amount)
get_buy()
If you’re wondering, store_items is a dictionary containing the store items like this:
store_items = {
"carrot": "carrot",
"apple": "apple",
"pear": "pear",
"taco": "taco",
"banana": "banana",
"tomato": "tomato",
"cranberry": "cranberry",
"orange": "orange",
}
Then store_rec is an empty list, like this:
store_rec = []
It contains recommendations for telling the user what to buy next time, but that’s not where I think the error is located. What I was trying to do in the line where the error appears to be, is append the amount of items specified by the user to the empty store_rec list. Unfortunatly, then I get the error. It seems like it should work, but it doesn’t. So, with that in mind, any help on my problem is appreciated!
Answers:
If you read the error carefully, it’s telling you what’s wrong — it’s nonsensical to multiply a sequence by a string; the string must be converted to an integer first.
Convert the amount to an integer by changing the line:
amount = raw_input(prompt)
to
amount = int(raw_input(prompt))
So, I was making a text-based “shopping” program, and I came across an error that didn’t quite make sense to me. The error message was this:
TypeError: can't multiply sequence by non-int of type str
Here is a code snippet of where I think the error is located (Comment above line where I believe error to be located):
def buy():
print "nType in a store item to buy."
find_item = raw_input(prompt)
if find_item in store_items:
print "Amount?"
amount = raw_input(prompt)
if amount:
#Error in next line?
store_rec.append(store_items[find_item] * amount)
get_buy()
If you’re wondering, store_items is a dictionary containing the store items like this:
store_items = {
"carrot": "carrot",
"apple": "apple",
"pear": "pear",
"taco": "taco",
"banana": "banana",
"tomato": "tomato",
"cranberry": "cranberry",
"orange": "orange",
}
Then store_rec is an empty list, like this:
store_rec = []
It contains recommendations for telling the user what to buy next time, but that’s not where I think the error is located. What I was trying to do in the line where the error appears to be, is append the amount of items specified by the user to the empty store_rec list. Unfortunatly, then I get the error. It seems like it should work, but it doesn’t. So, with that in mind, any help on my problem is appreciated!
If you read the error carefully, it’s telling you what’s wrong — it’s nonsensical to multiply a sequence by a string; the string must be converted to an integer first.
Convert the amount to an integer by changing the line:
amount = raw_input(prompt)
to
amount = int(raw_input(prompt))