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 trialHarihar Kandula
6,486 PointsI was able to get the code worked in workspace. However, the exercise here is telling its incorrect.
The below code executes fine in Workspace. The same isn't working in exercise. Thanks!
import random
def random_item(arg1):
rand_num = random.randint(0,(len(arg1)-1))
print("The randomly selected number is {}".format(rand_num))
arg1_list = list(arg1)
for letter in arg1_list:
if arg1_list.index(letter) == rand_num:
print("The return value would be {}".format(letter))
break
random_item("Treehouse")
2 Answers
Steven Parker
231,269 PointsThis code may not cause errors, but what it does is different from what the challenge instructions ask for:
- the challenge does not ask you to "print" anything
- the challenge does not require formatting strings
- the challenge does not require a loop
- the challenge does ask that the function return a single member based on a random index
Harihar Kandula
6,486 PointsThank you! I think i looked at the example given in "gray" and deluded. I will correct the code and retry. Thanks again!
Harihar Kandula
6,486 PointsSteven Parker - Thank you! I've corrected the code and got through the challenge.