简体   繁体   English

f 字符串未在列表内更新

[英]f string not updating inside a list

I'm trying to create the game Yahtzee in python, but I am having trouble putting the points in a table I made.我正在尝试在 python 中创建游戏 Yahtzee,但我无法将这些点放在我制作的表格中。 The table is a list with each item corresponding to one row.该表是一个列表,每个项目对应于一行。 Each item (meaning each row) is an f-string which retrieves a value from a dictionary inside curly brackets.每个项目(意味着每一行)都是一个 f 字符串,它从大括号内的字典中检索一个值。

Later in the code, I update the value that is being called on in the row, and print the row.稍后在代码中,我更新了行中被调用的值,并打印了该行。 For some reason, the old value from the dictionary is the one that gets printed.出于某种原因,字典中的旧值是被打印的值。

from random import randrange

def roll(saved):
    global roll
    roll = []
    for i in range(5 - len(saved)):
        roll.append(str(randrange(1,7)))
    global roll_og
    roll_og = roll
    roll.sort()
    print("Avaliable dice : " + ",".join(roll))

def avaliable_points():
#numbers
    global roll_og
    for i in range(1,7):
        if roll_og.count(str(i)) > 0:
            points[str(i)] = roll_og.count(str(i)) * i


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


saved = []

points = {
    "1" : "",
    "2" : "",
    "3" : "",
    "4" : "",
    "5" : "",
    "6" : ""
}

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


roll(saved)
avaliable_points()
print(points)
print_table(avaliable_grid)    

The table I care about is the available_grid list, and the dictionary is the points dictionary.我关心的表是available_grid列表,字典是points字典。 All the other functions in the code are the ones that determine how many points go into each slot in the table.代码中的所有其他函数都是确定表中每个插槽中有多少点 go 的函数。

Avaliable dice : 1,2,2,4,6
{'1': 1, '2': 4, '3': '', '4': 4, '5': '', '6': 6}
 _____________________________          _____________________________
|           |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|        |        |
 -----------------------------          ----------------------------- 

I printed the dictionary after I changed it in order to check if it has indeed been changed.我在更改字典后打印了字典,以检查它是否确实已更改。

The problem is that you're expecting the f strings to update when the points dictionary changes, but f strings are created immutably when interpreted.问题是您希望 f 字符串在points字典更改时更新,但 f 字符串在解释时创建不可变。 Look at this example:看这个例子:

>>> x = 10
>>> x_string = f"x is {x}"
>>> x_string
'x is 10'

>>> x = 20        # Change the value of x.
>>> x_string
'x is 10'

So one way to fix your problem is to define avaliable_grid within print_table() .因此,解决问题的一种方法是在print_table()中定义avaliable_grid

def print_table(points):
    available_grid = [ #...
                       #...
        ]
    for i in available_grid:
        print(i)

I also recommend passing in points so that you don't get mixed up with global variables.我还建议传入points ,以免与全局变量混淆。

By the way, since you only use the available_grid as a way to track the string, you can get the same result with a block string.顺便说一句,由于您仅使用available_grid作为跟踪字符串的一种方式,因此您可以使用块字符串获得相同的结果。

available_grid = f"""
 _____________________________          _____________________________
|           |Player 1|Player 2|        |           |Player 1|Player 2|
|-----------|--------|--------|        |-----------|--------|--------|
|Ones       |{points[str(1)]}|        |        |3 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Twos       |{points[str(2)]}|        |        |4 of a Kind|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Threes     |{points[str(3)]}|        |        |Full House |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fours      |{points[str(4)]}|        |        |S. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Fives      |{points[str(5)]}|        |        |L. Straight|        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sixes      |{points[str(6)]}|        |        |Chance     |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|#############################|        |YAHTZEE    |        |        |
|-----------|--------|--------|        |-----------|--------|--------|
|Sum        |        |        |        |#############################|
|-----------|--------|--------|        |-----------|--------|--------|
|Bonus      |        |        |        |TOTAL SCORE|        |        |
 -----------------------------          -----------------------------
"""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM