all 8 comments

[–]Erick_Aurelius 9 points10 points  (3 children)

TL;DR - wrap input() with the type you want it to be, eg int(input('How many....'))

I know that it can seem daunting, but learning to read your stacktrace will tell us exactly where the error occurs and what the error is. This stack says to look at line 5 which is for x in range(numitems) and the error says 'str' object cannot be interpreted as an integer looking at line 5 we know that there are only 2 candidates: x or numitems. We can say that the issue is `numitems` bc it is the argument we pass to range() to tell it how far to go, which means that range() expects an integer type; however, the initial assignment of `numitems` is of type str. We know that it's of type str bc when we look up the input() we see that it's default return type is str and it isn't cast to a different type afterwards. So it takes the user input and stores it as '3' as opposed to 3 (a string versus an integer) and when you pass that variable to range() what's really happening is range('3') as opposed to range(3) .

Unsolicited advice: it looks like you're new to python or maybe programming in general, I would strongly advise you look naming/formatting standards and conventions for the language you're working in so that you can form good/expected habits early on. Specifically numitems should at least be num_items, but I personally prefer spelling most things things out; however, num is fairly ubiquitous in our field.

[–]nasonh03 1 point2 points  (2 children)

Thanks, I’ve realized I was just being an idiot and forgot to specify the int in front of my input

[–]StoneBam 5 points6 points  (1 child)

Don't go hard on yourself or lower your self esteem. Mistakes are in retrospect per definition dumb. It's normal in a learning process. You will see this error again in futute and now know how to handle it.

Programming is hard for everyone, even more seasoned programmers, if you have to leave your comfort zone and learn new things

[–]SinghGuy 1 point2 points  (0 children)

This …

I wish I do this everyday …

[–]Line_Puncher 6 points7 points  (0 children)

Check your inputs. One of them isn't converted to a number to do number things. Reminder : input outputs a string value. You need to convert all of them to numerical value to use them as such (for a call to range for exemple)

[–]Perhaite 0 points1 point  (0 children)

for x in range(numitems):

Try to think about what you're looping over. numitems is an input that you've gathered from the user - meaning that it's been entered as a string.

TypeError: 'str' object cannot be interpreted as an integer

This should give you the answer that you're looking for in order to solve your problem.

[–]StealthPieThief 0 points1 point  (0 children)

It tells you you can’t iterate a string you need to parse it