Create Your Own (Mass Effect)


#1

https://www.codecademy.com/courses/python-beginner-en-cxMGf/2/6?curriculum_id=4f89dab3d788890003000096

I already finished the project, but I am having trouble wanting to make it unique for myself. I have a list of 20 names for my variable. What I want to do is the following: Rather than printing all 20 names, I would like for it to print a randomized (or shuffled) list of 3 names.

I've been wrestling with this issue for a while, searching different examples and ideas, but I've been stuck. Perhaps it's something so simple, but my mind is drawing a blank. Any tips would be great!

Here is my code:

import random

squad = ['Ashley', 'Kaiden', 'Liara', 'Tali', 'Garrus', 'Wrex', 'Shepard', 'Miranda', 'Mordin', 'Zaeed', 'Kasumi', 'Samara', 'Legion', 'Jacob', 'Grunt', 'Thane', 'Jack', 'James', 'EDI', 'Javik']

print random.shuffle(squad)
print "Your squad:"

for soul in squad:
    if soul == 'Legion':
        print "Does this unit have a soul?"
        break
    elif soul == 'Shepard':
        print "He's Commander Shepard! Space crime fighting and rocking the galaxy!"
        break
    print soul
else:
    print "The best of the best!"

#2

For this, I would use random.randint() in a for loop.
You want to print a list of 3 random names, so I would suggest running a for loop that runs 3 times and each time run random.randint() on the list to choose a random name. You would run the function as the index of the list.

Using random.randint():

This function takes two parameters: minimum and maximum. The random number will be above or equal to the minimum number, and ONLY lower than the maximum. This means you can write it like this: random.randint(0,20). It will not return an out of bounds error.

The final code will look something like this:

for x in range(3):
    print squad[random.randint(0,20)]

Implement it how you wish.

Hope this helps :slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.