Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random selection
#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


Messages In This Thread
Random selection - by EMarburg - Aug-11-2019, 11:43 PM
RE: Random selection - by boring_accountant - Aug-12-2019, 12:11 AM
RE: Random selection - by EMarburg - Aug-12-2019, 12:22 AM
RE: Random selection - by boring_accountant - Aug-12-2019, 12:48 AM
RE: Random selection - by EMarburg - Aug-12-2019, 01:02 AM
RE: Random selection - by boring_accountant - Aug-12-2019, 01:14 AM
RE: Random selection - by ichabod801 - Aug-12-2019, 01:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  random selection of song and artist not working Unknown_Relic 2 2,385 Nov-08-2019, 02:06 PM
Last Post: perfringo
  Random number selection stumunro 4 3,676 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