Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMark Baek
1,172 PointsChecking work results in: TypeError: unsupported operand type(s) for -: 'list' and 'int'. Running in shell does not.
The purpose of this code is to recreate the random.choice method.
When I check my work, I get the "TypeError: unsupported operand type(s) for -: 'list' and 'int'" message.
When I run this in Python shell, I don't get any error message. However I don't get any output from the shell either when I change "return" to "print".
Any hints? I used brackets instead of parentheses on line 6 as I believe that's what you need to do to get the character located at that index?
import random
def random_item(item):
item_selector = random.randint(0, len(item - 1))
return(item.index[item_selector])
1 Answer
Carlos Federico Puebla Larregle
21,074 PointsJust use the "item_selector" variable as the index of your item list, and put your "- 1" out of the parenthesis of the "len" function, like this:
import random
def random_item(item):
item_selector = random.randint(0, len(item) - 1)
return item[item_selector]
I hope that helps a little bit.
Mark Baek
1,172 PointsMark Baek
1,172 PointsThanks, that helps a lot. What this tells me is that I need to focus on learning better syntax, and that it's too early and I need to take a break!