Concatenating a dictionary item with a string inside a list

Viewed 69

so I'm trying to create the game Yahtzee in python, and I'm trying to put up points inside a table I created. although, because strings are immutable, I tried to retrieve a dictionary item from inside the list (which comprises the table) and concatenate it with the rest of the string. but for some reason my output just excludes the dictionary item as if it's not there.

here is the code:

points = {
    "1" : "Ones",
    "2" : "Twos",
    "3" : "Threes",
    "4" : "Fours",
    "5" : "Fives",
    "6" : "Sixes"
}


avaliable_grid = [
    " _____________________________          _____________________________",
    "|           |Player 1|Player 2|        |           |Player 1|Player 2|",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Ones       |" + points["1"] + "|        |        |3 of a Kind|        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Twos       |" + points["2"] + "|        |        |4 of a Kind|        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Threes     |" + points["3"] + " |        |        |Full House |        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Fours      |" + points["4"] + "|        |        |S. Straight|        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Fives      |" + points["5"] + "|        |        |L. Straight|        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Sixes      |" + points["6"] + "|        |        |Chance     |        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|#############################|        |YAHTZEE    |        |        |",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Sum        |        |        |        |#############################|",
    "|-----------|--------|--------|        |-----------|--------|--------|",
    "|Bonus      |        |        |        |TOTAL SCORE|        |        |",
    " -----------------------------          ----------------------------- ",
    ]


def print_table(table):
    for i in table:
        print(i)

print_table(avaliable_grid)    

output:

 _____________________________          _____________________________
|           |Player 1|Player 2|        |           |Player 1|Player 2|
|-----------|--------|--------|        |-----------|--------|--------|
|Ones       |        |        |        |3 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Twos       |        |        |        |4 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Threes     |        |        |        |Full House |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fours      |        |        |        |S. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fives      |        |        |        |L. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sixes      |        |        |        |Chance     |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|#############################|        |YAHTZEE    |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sum        |        |        |        |#############################|
|-----------|--------|--------|        |-----------|--------|--------|
|Bonus      |        |        |        |TOTAL SCORE|        |        |
 -----------------------------          ----------------------------- 

for some reason, the points[] dictionary is ignored. also, the way I am printing this is like that because I want it to print row by row, without parenthesis, or quotation marks.

for reference- I am using python 3.8.2, and I'm working through IDLE.

1 Answers

Since I'm new and can't comment I'm just going to guess what the rest of your code could look like. I started by adding this to try and reproduce your output.

for row in avaliable_grid:
    print(row)

The output:

_____________________________          _____________________________
|           |Player 1|Player 2|        |           |Player 1|Player 2|
|-----------|--------|--------|        |-----------|--------|--------|
|Ones       |Ones|        |        |3 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Twos       |Twos|        |        |4 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Threes     |Threes |        |        |Full House |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fours      |Fours|        |        |S. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fives      |Fives|        |        |L. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sixes      |Sixes|        |        |Chance     |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|#############################|        |YAHTZEE    |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sum        |        |        |        |#############################|
|-----------|--------|--------|        |-----------|--------|--------|
|Bonus      |        |        |        |TOTAL SCORE|        |        |
 -----------------------------          ----------------------------- 

So, in my output, I think your code works as intended. Could you explain what you used to actually print the output?

edit: obviously there's still some formatting to do, maybe you could try using the pprint or \t, or maybe the simplest thing would be to just add trailing space characters to each of your dictionary values to have their lengths matching and adjust the card accordingly.

points = {
    "1" : "Ones  ",
    "2" : "Twos  ",
    "3" : "Threes",
    "4" : "Fours ",
    "5" : "Fives ",
    "6" : "Sixes "
}

edit2: since you're using python 3.8, you could use formatted string literals (f-strings). They're a really fast and readable way to format your strings dynamically. You do this by adding curly braces around python code within a string and prepending an f character before opening the string like so:

f"|Ones       | {points["1"]} |        |        |3 of a Kind|        |" 
Related