Python Python Basics (2015) Letter Game App Random Item

Nikhil Alexander
Nikhil Alexander
1,444 Points

this code does seem to pass the challenge

im not sure whether to subtract one from the length of the iterable or from the random number.. if im making some other mistake please help me correct it

item.py
import random 

random_num = random.randint(0, 7)

def random_item(Avengers):
    return(Avengers[random_num])

2 Answers

Steven Parker
Steven Parker
177,888 Points

The random number should be selected inside the function, and the range should be based on the size of the argument instead of any fixed value.

Nikhil Alexander
Nikhil Alexander
1,444 Points

thank you so much the code passes thank you again for your help!!

Dan Garrison
Dan Garrison
22,438 Points

The code challenge is asking you to give a random number between 0 and the length of the iterable provided as the argument minus 1. To do this you need to use the built in python function, len() to determine the length of the incoming argument. Remember, functions are reusable code snippits. They are useful because you don't always know what the input will be. In this case they could give you an iterable (string, list, etc) that is 10 characters in length and another time you could get an iterable that 15 characters in length. Your function should be capable of handling both.

You should not be declaring any variables outside your function. In fact, you can even complete the challenge without declaring a variable.

Nikhil Alexander
Nikhil Alexander
1,444 Points

thank you.....i understood the concept