Home > Back-end >  How do I prevent elements in a nested list to appear more than once?
How do I prevent elements in a nested list to appear more than once?

Time:01-27

I am trying to create a simple guess the number game with a scoreboard. However, I am finding it difficult to not create a new player (name) for a recurring player. May I know what is wrong with my coding or should I do something different so that a recurring player's name won't appear more than once in the list?

lst=[["Player 1",0],['Player 2',0],['Player 3',0],['Player 4',0],['Player 5',0]]

#increase score
def inc(I):
    if lst[0][0]== playername:
         lst[0][1] =i
    elif lst[1][0]==playername:
        lst[1][1] =i
    elif lst[2][0]==playername:
        lst[2][1] =i
    elif lst[3][0]==playername:
        lst[3][1] =i
    elif lst[4][0]==playername:
        lst[4][1] =i
    return

secret = 0
guess= -1
play="yes"

#name for player
playername=str(input("Player B, what is your name?"))
if lst[0][0]=="Player 1":
    if lst[0][0]!= playername:
        lst[0][0]=playername
elif lst[1][0]== "Player 2":
    if lst[1][0]!= playername:
        lst[1][0]=playername
elif lst[2][0]=="Player 3":
    if lst[2][0]!= playername:
        lst[2][0]=playername
elif lst[3][0]=="Player 4":
    if lst[3][0]!= playername:
        lst[3][0]=playername
elif lst[4][0]=="Player 5":
    if lst[4][0]!= playername:
        lst[4][0]=playername

while secret>100 or secret <1:
    secret = float(input("Player A: Please input a secret number between 1-100: "))
while(guess!=secret):
    guess = float(input("Player B: Please guess again: "))
    guess1(secret, guess)
    tries =1
    if tries==4:
        break

This is the output:

[['Sherlyn', 2],
 ['sherlyn', 1],
 ['Sherlyn', 0],
 ['Player 4', 0],
 ['Player 5', 0]]

CodePudding user response:

I don't think there's anything wrong with your code per se, it's just that the codes definition of 'same name' is different than yours.

From a design standpoint, I would use python's dictionaries for this: https://docs.python.org/3/tutorial/datastructures.html#dictionaries

However, you may not have learned them yet, or are simply wanting practice with arrays, which are both good reasons to use arrays.

Then, for the names themselves, you're doing this comparison lst[0][0]=="Player 1". Is that enough though? What happens when the player comes back? (it creates a new name).

Instead, you should first check whether the name (playername) is already in your existing list (additionally, it would be better to do this in a loop rather than hardcoding indexes 0-4), and THEN check if the name is one your default values to be updated.

Finally, I would probably store both the 'as entered' name, and an identifier name that you cast to upper case or lower case (such as with https://www.w3schools.com/python/ref_string_lower.asp) to prevent Sherlyn being distinct from sherlyn.

CodePudding user response:

I think you may change the codes testing player name to the following:

if lst[0][0] != playername and lst[1][0] != playername and lst[2][0] != playername and lst[3][0] != playername and lst[4][0] != playername:
    if lst[0][0] == "Player 1":
        lst[0][0] = playername
    elif lst[1][0] == "Player 2":
        lst[1][0] = playername
    elif lst[2][0] == "Player 3":
        lst[2][0] = playername
    elif lst[3][0] == "Player 4":
        lst[3][0]! = playername
    elif lst[4][0] == "Player 5":
        lst[4][0] = playername
    else:
        # a sixth name's entered
        pass # may also output a message
    
  •  Tags:  
  • Related