Home > Blockchain >  How to assign points in a simple game
How to assign points in a simple game

Time:11-26

I am making a simple game, at the beginning you can choose the amount of players, which is between 2 and 5 (shown below) I am having problem with assigning the initial amount of points, which is 100 points. Also, not sure where to place the code regarding the points in my woring code below. When I start working on the game, after each dice moevent the score would increase.

players_list= []
max_players= 5
max_players = int(input(" Please, insert the number of players? : "))
while (max_players <2) or (max_players > 5) :
  max_players = int(input(" Number of players must be between 2 and 5.Number of players ?"))
players_list = []
while len(players_list) < max_players:
    player1 = input(" Enter your first and last name? : ")
    players_list.append(player1)
    print("Players in the game : ")
print(players_list)

Should I change the players list into a dictionary?

The code with the score system that does not work

score=100
players_list= []
max_players= 5
max_players = int(input(" Please, insert the number of players? : "))
while (max_players <2) or (max_players > 5) :
  max_players = int(input(" Number of players must be between 2 and 5.Number of players ?"))
players_list = []
while len(players_list) < max_players:
    player1 = input(" Enter your first and last name? : ")
    players_list.append(player1)
    print("Players in the game : ")
    players_list.appened (players)= { score:100}

print(players_list)
print(score)

CodePudding user response:

I'd recommend to use dictionary where keys are player names (assuming player names will be unique) and values will be player's score:

players_dict = {}
score = 100

max_players = -1
while not (2 <= max_players <= 5):
    max_players = int(input("Please, insert the number of players: "))

while len(players_dict) < max_players:
    player = input("Enter your first and last name: ")
    if player in players_dict:
        print(f"Player {player} already exists, choose another name")
    else:
        players_dict[player] = score

print(players_dict)

Prints (for example):

Please, insert the number of players: 1
Please, insert the number of players: 3
Enter your first and last name: John
Enter your first and last name: Adam
Enter your first and last name: John
Player John already exists, choose another name
Enter your first and last name: Lucy
{'John': 100, 'Adam': 100, 'Lucy': 100}

CodePudding user response:

No you don't need to change the list into a dictionary. Rather you probably want to have a list of dictionaries. TLDR;

I'm not sure if I understand your problem, cause the description is vague tbh.

  1. There's no need to use the input before the while loop.
  2. The user can input something what cannot be parsed into an int, so I'd wrap it into try...except block.
  3. "player_list" is redefined
  4. in the 2nd loop it's not player1, it's the "next" player I guess
  5. you can also keep name and surname in a single string and then skip the split
  6. one way or another it would make sense to keep your player as a dict
  7. Consider renaming players_list to players, typically you don't add the name of the data structure to the variable name

Code:

score=100
players_list= []
max_players= 0
while (max_players <2) or (max_players > 5) :
    try:
        max_players = int(input(" Number of players must be between 2 and 5.Number of players ?"))
    except Exception as e:
        print(f"Invalid input: {e}")
while len(players_list) < max_players:
    name, surname = input(" Enter your first and last name? : ").split(" ")
    player = {"name": name, "surname": surname, "points": score}
    players_list.append(player)
    print(f"Players in the game : {players_list}")
  • Related