python 3.x - Doesn't accept the list index? -


i have peice of code:

n = int (input ('enter number of players: ')) m = [[j] j in range (0, n)] all_names= [] = 0 while n > 1:     m[i] = input('player {0}: '.format (i+1))     all_names.extend ([m[i]])     if m[i][0] != m[i-1][-1]:         b= m.pop (i)         n = n-1     if all_names.count (m[i]) == 2:         n = n-1         b= m.pop (i)     = i+1 

it says index out of range (second if clause) dont it, why?

i hate not answer question directly, you're trying seems... confusing. python has sort of rule there's supposed clear, clean way of doing things, if piece of code looks funky (especially such simple function), it's not using right approach.

if want create container of names, there numerous simpler ways of doing it:

players=int(input("how many players?\n")) player_names=set() while len(player_names)<players:   player_names.add(input("what player {}'s name?\n".format(len(player_names)+1))) 

... give set of unique player names, although won't ordered. might matter (your implementation kept order, maybe is), , in case still use list , add small check make sure adding new name , not repeatedly adding names:

players=int(input("how many players?\n")) player_names=list() while len(player_names)<players:   playname=input("what player {}'s name?\n".format(len(player_names)+1))   if playname not in player_names:     player_names.append(playname) 

i'm open haranguing me dodging question, particularly if there's purpose/reason approach questioner took.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo