-1

I'am trying to create a definition that assigns objects in a list to variables but unfortunately it is not working:

when I try to print player_1 (as in the last step) it gives me a

NameError

Any suggestion or feedbacks on how to make the definition shorter or better is always welcome. The whole project( it is till the beginning) is on https://github.com/ahmadkurdo/project---a

If you have time and look at it and give me some feedback on it would be appreciated.

def assign_players(list_of_names):
    if len(list_of_names) == 2:
        player_1 = list_of_names[0]
        player_2 = list_of_names[1]

    elif len(list_of_names) == 3:
        player_1 = list_of_names[0]
        player_2 = list_of_names[1]
        player_3 = list_of_names[2]

    elif len(list_of_names) == 4:
        player_1 = list_of_names[0]
        player_2 = list_of_names[1]
        player_3 = list_of_names[2]
        player_4 = list_of_names[3]

    elif len(list_of_names) == 5:
        player_1 = list_of_names[0]
        player_2 = list_of_names[1]
        player_3 = list_of_names[2]
        player_4 = list_of_names[3]
        player_5 = list_of_names[4]

    elif len(list_of_names) == 6:
        player_1 = list_of_names[0]
        player_2 = list_of_names[1]
        player_3 = list_of_names[2]
        player_4 = list_of_names[3]
        player_5 = list_of_names[4]
        player_6 = list_of_names[5]


number_of_players = int(input('How many players are playing?  '))
list_of_players = []

while number_of_players > 0:
    name_player = input('What is your name  ')
    list_of_players.append(name_player)
    number_of_players = number_of_players - 1

assign_players(list_of_players)

print(player_1)
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Ahmed
  • 119
  • 1
  • 13

1 Answers1

0

Your problem is the scope of your variables. Scope means: where is my variable defined/visible and when is it no longer defined.

If you define a variable inside a function (like you do) it is only known inside this function - you can not access it once you leave the function.

The variable is unknown - hence NameError.

You can however return it and by assign it to some other variable as return of your function.

You can work around your specific problem (and get rid of those if statements) by simplifying your code like this:

number_of_players = int(input('How many players are playing?  '))
list_of_players = []

for _ in range(number_of_players):
    list_of_players.append(input('What is your name  '))

player_1,player_2,player_3,player_4,player_5,player_6, *rest = list_of_players + [None]*5

print(list_of_players + [None] * 5) 
print(player_1)
print(player_2)
print(player_3)
print(player_4)
print(player_5)
print(player_6)
print(rest)

Output for 2 + 'jim' + 'bob':

['jim', 'bob', None, None, None, None, None] # filled up with [None] * 5
jim
bob
None
None
None
None
[]

The code works by filling up your list to the needed amount of items (using [None] for any not inputted) so that you can decompose the list again into your variables. BUT it would be much easier to leave them in a list:

for round in range(1,10):
    for player in list_of_players:
        print (player, "'s turn:")
        # do something with this player

This is kindof difficult to do if you want to use your player_X variables instead and would lead to lots of duplicate code and you still would have to check if your player_X are filled or None ...

Read more about:

As function:

def assign_players(p): 
    return p + [None]*5

p1,p2,p3,p4,p5,p6,*rest = assign_players(list_of_players)
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69