Python Forum
How to assign a "Score" variable to each element in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to assign a "Score" variable to each element in a list
#1
Hey all, I was tasked to create a game for homework but I still can't seem to figure out how to assign each individual player a "Score" variable that changes as the game progresses. E.g.

If user inputs that there are 3 players, later in which the 3 players' names are appended into a list, how would I assign each player a individual variable in which it contains their respective scores?

import random
count = 0
playerList = []
def rollTurn():
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    print("Your first dice rolled a: {}".format(dice1))
    print("Your second dice rolled a: {}".format(dice2))
print("-------------------------MAIN MENU-------------------------")
print("1. Play a game \n2. See the Rules \n3. Exit")
print("-------------------------MAIN MENU-------------------------")
userAsk = int(input("Enter number of choice"))
if userAsk == 1:
    userNun=int(input("Number of players?\n> "))
    while len(playerList) != userNun:
        userNames=input("Please input name of player number {}/{}\n> ".format(len(playerList)+1, userNun))
        playerList.append(userNames)
    random.shuffle(playerList)
    print("Randomizing list..:",playerList)
    print("It's your turn:" , random.choice(playerList))
    rollTurn()
    while count < 900000: 
        rollAgain=input("Would you like to roll again?")
        if rollAgain == "Yes":
            count = count + 1
            rollTurn()
        elif rollAgain == "No": 
            print("You decided to skip")
            break
This is what I currently have that pertains to this specific task. So what should I add so that each player/element in playerList is given a variable that contains their respective scores? Also(Sorry for asking multiple questions), as this game is round based, as I'm randomizing the order in which the players play, how would I that after each player is done, it picks the player directly after the current player, in other words, how would I pick the element directly after? I know there's a function for that but I can't seem to remember... Thanks for entertaining my ignorance and any help would be much appreciated, thanks :)
Reply
#2
This is how I handle it: I have a dictionary of player names to their scores, like {'Bob': 23, 'Ichabod': 81, 'Buckaroo': 55}. I also have a list of the players, and an index pointing to that list for the current player. Each time through the loop you update the index, and if it's equal to the length of the list you reset it to zero (really easy to do with the % operator). For a simple repeated loop through a player list, there are better options, but they involve the itertools package, which is probably beyond the scope of your homework.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(May-15-2019, 01:19 PM)ichabod801 Wrote: This is how I handle it: I have a dictionary of player names to their scores, like {'Bob': 23, 'Ichabod': 81, 'Buckaroo': 55}. I also have a list of the players, and an index pointing to that list for the current player. Each time through the loop you update the index, and if it's equal to the length of the list you reset it to zero (really easy to do with the % operator). For a simple repeated loop through a player list, there are better options, but they involve the itertools package, which is probably beyond the scope of your homework.

How would I replace my list with this dict? Would I use playerList = {}? I have no experience with dictionaries in Python and wouldn't know how to implement such a function, how would I give each player a score in the dictionary? And how would I manipulate their individual scores as the game changes?
Reply
#4
If you have not been introduced to dictionary's yet you could alter your player list to have lists inside of a list.
playerList = [['player1', 5], ['player2', 8]]
Reply
#5
(May-16-2019, 07:37 AM)Yoriz Wrote: If you have not been introduced to dictionary's yet you could alter your player list to have lists inside of a list.
playerList = [['player1', 5], ['player2', 8]]
Wait, I don't know if I'm misinterpreting this but just to clarify, the code first asks the main user to enter the names of the other players, which are then appended into playerList. How would I then from the different names inside the list give each name a "score" variable that all starts from 0 and eventually gain points through the total value of the rolled dices? Is player1 an index or something, this thing should be simple but I'm just getting confused
Reply
#6
I think what Yoriz was suggesting is a list of sub-lists, where each sub-list is the name of the player and their score. So say you have a currentPlayer variable, which is the index of Yoriz's playerList for the current player. So at the beginning of the game, currentPlayer is 0 (for the first player). The name of the player is playerList[currentPlayer][0] ('player1') and the player's score is playerList[currentPlayer][1] (5).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to update score value? Mavoz 5 2,390 Nov-08-2022, 12:37 AM
Last Post: Mavoz
  How do you find the length of an element of a list? pav1983 13 4,777 Jun-13-2020, 12:06 AM
Last Post: pav1983
  Methods that return the highest score from the list erfanakbari1 7 7,053 Mar-26-2019, 08:32 PM
Last Post: aankrose
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,464 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,533 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  Average score MartinEvtimov 5 6,717 Apr-02-2017, 07:35 PM
Last Post: ichabod801
  maximum and minimum element from the list and return output in dict MeeranRizvi 1 3,699 Jan-02-2017, 02:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020