Issue
my error:
if player_order_chooser[4] == player_a: IndexError: list index out of range
>import random
>
>player_a = "Joe"
>player_b = "john"
>player_c = "Donald"
>player_d = "Brady"
>
>
>player_order_chooser = [player_a,player_b,player_c,player_d]
>random.shuffle(player_order_chooser)
>x = len(player_order_chooser)
>print(x)
>print(player_order_chooser[1])
>print(player_order_chooser[2])
>print(player_order_chooser[3])
>print(player_order_chooser[4])
Solution
The index of array/list starts from 0 instead of 1.
If you have array of length 4, that's mean it has values stored in indexes from 0 to 3.
In your example, without using:
random.shuffle(player_order_chooser)
In order to print "Joe" which is stored in string player_a:
>print(player_order_chooser[0])
While, in order to print "Brady" which is stored in string player_d:
>print(player_order_chooser[3])
After using random.shuffle() the results will change but the size of index remains the same.
Answered By - DaSoliman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.