Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random selection
#1
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
?????
Reply
#2
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
Reply
#3
I am using 2.x and print player does not work. I just gives me a random letter
Reply
#4
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 ?
Reply
#5
#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
Reply
#6
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))
Output:
abc def def e
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))
Output:
abc def ['abc', 'def'] def
Hope this helps.
Reply
#7
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 pirates
You should also switch to Python 3. At the end of the year support for 2.7 will stop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  random selection of song and artist not working Unknown_Relic 2 2,317 Nov-08-2019, 02:06 PM
Last Post: perfringo
  Random number selection stumunro 4 3,611 Aug-31-2017, 02:12 PM
Last Post: stumunro

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020