Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I use the .random module on a list to get a random item out without using .choice?

I’m a total begginer in programming πŸ™‚

I’m trying to solve a coding challange which instructs me to
pick a random name out of a list but i’m not allowed to use the .choice function
instead I somehow need to pick an item out using the len() function
in my code.

# Split string method
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
# 🚨 Don't change the code above πŸ‘†

#Write your code below this line πŸ‘‡
import random

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

result = names[random.randrange(len(names))]

This is also a possible solution:

result = names[random.randint(0, len(names) - 1)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use