Posts: 3
Threads: 1
Joined: Aug 2019
Aug-11-2019, 11:43 PM
(This post was last modified: Aug-12-2019, 01:11 AM by ichabod801.)
I am taking a class and I am having trouble with one line of script. I have my List as follows:
1 2 3 4 5 6 7 8 9 10 11 |
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
1 2 |
player = random.choice(pirates)
opponent = random.choice(pirates)
|
what script do I use to print the player and opponent?
I have tried print player
?????
Posts: 27
Threads: 1
Joined: Feb 2018
I would assume your class is using python 3.x, where the proper syntax is:
1 2 |
print (player)
print (opponent)
|
If using python 2.x, then:
1 2 |
print player
print opponent
|
Posts: 3
Threads: 1
Joined: Aug 2019
I am using 2.x and print player does not work. I just gives me a random letter
Posts: 27
Threads: 1
Joined: Feb 2018
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 ?
Posts: 3
Threads: 1
Joined: Aug 2019
Aug-12-2019, 01:02 AM
(This post was last modified: Aug-12-2019, 01:13 AM by ichabod801.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import random
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
for pirates in pirates:
print pirates
attack = [ 'Dodge' , 'Parry' , 'Thrust' ]
print
print
for attack in attack:
print attack
player = random.choice(pirates)
opponent = random.choice(pirates)
print "Ahoy ye swabs! Prepare for battle!"
print "PLAYER has challenged OPPONENT in one on one combat!"
pattack = random.choice(attack)
oattack = random.choice(attack)
|
Posts: 27
Threads: 1
Joined: Feb 2018
Aug-12-2019, 01:14 AM
(This post was last modified: Aug-12-2019, 01:16 AM by boring_accountant.)
Ah ! Here is the error:
1 2 3 4 5 |
for pirates in pirates:
print pirates
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:
1 2 3 4 5 |
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:
1 2 3 4 5 |
tests = [ 'abc' , 'def' ]
for test in tests:
print (test)
print (tests)
print (random.choice(tests))
|
Output: abc
def
['abc', 'def']
def
Hope this helps.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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:
1 2 |
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.
|