Bottom Page

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 :)
Quote
#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

Quote
#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?
Quote
#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]]
Quote
#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
Quote
#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

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Methods that return the highest score from the list erfanakbari1 7 243 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 106 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 132 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  how to assign variables? RY8 2 208 Mar-09-2019, 01:59 PM
Last Post: saravanatn
  making a dictionary from a list, one key with multiple values in a list within a list rhai 4 448 Oct-24-2018, 06:40 PM
Last Post: LeSchakal
  Element Tree Xpath hey_arnold 9 754 Aug-09-2018, 02:53 PM
Last Post: hey_arnold
  ValueError: setting an array element with a sequence ying9309 2 2,933 Jul-15-2018, 07:04 AM
Last Post: ying9309
  dummy classifier accuracy and recall score metalray 0 1,422 Oct-31-2017, 09:27 AM
Last Post: metalray
  python machine learning model, macro precision score? metalray 5 1,902 Sep-28-2017, 09:13 PM
Last Post: nilamo
  Average score MartinEvtimov 5 1,828 Apr-02-2017, 07:35 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)