Random selection - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Random selection (/thread-20449.html) |
Random selection - EMarburg - Aug-11-2019 I am taking a class and I am having trouble with one line of script. I have my List as follows: pirates = [ 'Captain William Kidd', 'Pierre Le Grand', 'Red Leg Grieves', 'Edward Low', 'Calico Jack Rackham', 'Anne Bonny', 'Captain Henry Morgan', 'Black Sam Bellamy', 'Black Bart Roberts', 'Edward Blackbeard Teach']Then I have player = random.choice(pirates) opponent = random.choice(pirates)what script do I use to print the player and opponent? I have tried print player ????? RE: Random selection - boring_accountant - Aug-12-2019 I would assume your class is using python 3.x, where the proper syntax is: print(player) print(opponent)If using python 2.x, then: print player print opponent RE: Random selection - EMarburg - Aug-12-2019 I am using 2.x and print player does not work. I just gives me a random letter RE: Random selection - boring_accountant - Aug-12-2019 I can run your code just fine using 3.x. I don't have a working installation of 2.x at hand. Can you post your full code, just to make sure the issue doesn't lie elsewhere ? RE: Random selection - EMarburg - Aug-12-2019 #Import the random Module import random # Here are your list of Pirates pirates = [ 'Captain William Kidd', 'Pierre Le Grand', 'Red Leg Grieves', 'Edward Low', 'Calico Jack Rackham', 'Anne Bonny', 'Captain Henry Morgan', 'Black Sam Bellamy', 'Black Bart Roberts', 'Edward Blackbeard Teach'] # PRINT THE CONTENT OF THE PIRATES LIST TO THE SCREEN HERE print '''Your List of Pirates are: ''' for pirates in pirates: print pirates # Attacks List attack = ['Dodge', 'Parry', 'Thrust'] print''' ''' # PRINT THE CONTENTS OF THE ATTACK LIST TO THE SCREEN HERE print '''Your Available Attacks are: ''' for attack in attack: print attack # Choosing the Characters for the fight player = random.choice(pirates) opponent = random.choice(pirates) # ############################################################################# # Change the line below to correctly concatenate PLAYER and OPPONENT with # the variables above so that when the statement prints to screen, the chosen # character names are shown. # ############################################################################# print "Ahoy ye swabs! Prepare for battle!" print "PLAYER has challenged OPPONENT in one on one combat!" # Choosing the attack pattack = random.choice(attack) oattack = random.choice(attack) # CREATE A CORRECTLY CONCATENATED STRING THAT CONTAINS PLAYER, OPPONENT, PATTACK & OATTACK RE: Random selection - boring_accountant - Aug-12-2019 Ah ! Here is the error: for pirates in pirates: # <-- This line is the bad guy print pirates # Attacks List attack = ['Dodge', 'Parry', 'Thrust'] print'''What this does is it cycles through values in the list named pirates and puts the result in a string called pirates. This effectively replaces the initial list with a string. When you call random.choice(), it is correctly applied on a string, which will return a random letter from the last word in the initial pirates list. Here is a short example that should demonstrate what I mean: test = ['abc', 'def'] for test in test: print(test) print(test) print(random.choice(test)) If rename my initial variable from test to tests, here is the resulting code and output:tests = ['abc', 'def'] for test in tests: print(test) print(tests) print(random.choice(tests)) Hope this helps.
RE: Random selection - ichabod801 - Aug-12-2019 I believe the problem is line 21: for pirates in pirates: . You use the plural form for both the looping variable and the thing looped over. So at the end, pirates is equal to the last pirate: 'Edward Blackbeard Teach'. So you should switch that loop to:for pirate in pirates: print piratesYou should also switch to Python 3. At the end of the year support for 2.7 will stop. |